简体   繁体   English

私有 static 字段 Java

[英]private static field Java

I was asked the following question:我被问到以下问题:

Assuming all instance fields and all instance methods of class A in Java are private, which of the following are correct:假设 Java 中 class A 的所有实例字段和所有实例方法都是私有的,以下哪项是正确的:

  1. A is immutable A 是不可变的
  2. A is not for sure immutable because it could be that it extends a not-immutable class A 不确定是不可变的,因为它可能扩展了一个不可变的 class
  3. A is not for sure immutable because you might be able to change its fields from static methods A 不确定是不可变的,因为您可能能够从 static 方法更改其字段

I thought the correct answer was 1 but turns out 2 and 3 are both correct and 1 is not.我认为正确的答案是 1,但结果 2 和 3 都是正确的,而 1 不是。

How come if everything is private you can still modify the fields?如果所有内容都是私有的,您仍然可以修改字段怎么办?

Why if it extends a not immutable class, but still have everything private, it might be now immutable?为什么如果它扩展了一个不可变的 class,但仍然拥有私有的一切,它现在可能是不可变的?

What does it mean to change its fields from static methods?从 static 方法更改其字段是什么意思?

A could extend a class with fields that are public, in which case, those fields can be modified despite A itself not defining any mutable fields, as it will inherit fields and instance methods from its parent class. A 可以使用公共字段扩展 class,在这种情况下,尽管A本身没有定义任何可变字段,但仍可以修改这些字段,因为它将从其父 class 继承字段和实例方法。

Consider the following code:考虑以下代码:

class Child extends Parent {
    private String name;
}
class Parent {
    public int id;
}
class TestChild {
    public static void main(final String[] args) {
        final Child child = new Child();
        //String s = child.name;<--The field Child.name is not visible
        System.out.println("Previous id: " + child.id);
        child.id = 100;//<--We can modify this because it is defined as public in Parent
        System.out.println("Updated id: " + child.id);
    }
}

The output will be: output将是:

Previous id: 0
Updated id: 100

For the next case, it seems that the question meant methods as in instance methods, not specifying whether or not there are static methods.对于下一种情况,问题似乎是实例方法中的方法,而不是指定是否存在 static 方法。 Static methods that are public can be called anywhere and create side effects by modifying static fields and can also access private instance fields on instances of A , which makes A not immutable.公开的 Static 方法可以在任何地方调用并通过修改 static 字段产生副作用,还可以访问A实例上的私有实例字段,这使得A不是不可变的。

Point 2:第 2 点:

class B {
    public int bValue;
}

class A extends B {
    private int aValue;
}

An instance of A is not immutable because it has inherited a bValue field that can be changed. A 的实例不是不可变的,因为它继承了可以更改的bValue字段。


Point 3:第 3 点:

class A {
    private int x;

    public static void mutate(A a) {
        a.x += 1;
    }
}

Instances of A can be mutated by calling the static method A.mutate , which has full access to A's private fields.可以通过调用 static 方法A.mutate来改变 A 的实例,该方法可以完全访问 A 的私有字段。 The stipulation as it now reads, "all instance methods of class A in Java are private", does not apply to static methods.现在的规定是,“Java 中 class A 的所有实例方法都是私有的”,不适用于 static 方法。

All the previous answers were right.前面的答案都是对的。 On the question regarding关于关于

  1. A is not for sure immutable because you might be able to change its fields from static methods A 不确定是不可变的,因为您可能能够从 static 方法更改其字段

can be achieved like可以像这样实现

public class Test {
public static void main(String[] args) {
    A a = new A(10);
    System.out.println(a);
    A.mutateObj(20);
    System.out.println(a);
 }
}

class A {
 private static A a;
 private int x;

 A(int x) {
    this.a = this;
    this.x = x;
 }

 public static void mutateObj(int b) {
    a.x = b;
 }

 @Override
 public String toString() {
    return "A{" +
            "x=" + x +
            '}';
 }
}

Result =>结果 =>

A{x=10}
A{x=20}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM