在Java中,有什么区别:

private final static int NUMBER = 10;

and

private final int NUMBER = 10;

两者都是私有的和final的,不同的是静态属性。

更好的是什么?,为什么?


当前回答

"Static" keyword makes the variable property of the class rather than individual instances of the class. There will be one copy of that variable that is shared amongst all the instances of that class. Any change in the state of the static variable will be reflected across all the instances. Add final to static and we get a variable that has been initialised once and for all at the class loading time and cannot be changed later by any instance of the class. Static final variables need to be initialised at the declaration time else we have compile time error. As far as private instance field is concerned, it refers to the property /state of an object /instance of a class. Each instance /object of the class will have its own copy of instance variable. When instance variable is declared final, it means we cannot change its value for this instance. For this we need to initialise the final variable either at declaration or in the constructor.If its not done in either of them, compile time error will show. Once initialised, if you try to reassign a value you will get a compile time error. Use static final variables where the data will be shared across all the instances of the class and you want the data to be read only.Use instance final variable if you want to represent some data that belongs to a each individual instance of the class but once stored cannot be changed. Usage of static and instance key word depends upon your design needs and what that data represents in the domain. If the data is used across the class instances then there is no need for individual copies/memory references for each object.

其他回答

正如Jon已经说过的,静态变量,也称为类变量,是跨类实例存在的变量。

我在这里找到了一个例子:

public class StaticVariable
{
  static int noOfInstances;
  StaticVariable()
  {
    noOfInstances++;
  }
  public static void main(String[] args)
  {
    StaticVariable sv1 = new StaticVariable();
    System.out.println("No. of instances for sv1 : " + sv1.noOfInstances);

    StaticVariable sv2 = new StaticVariable();
    System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
    System.out.println("No. of instances for st2 : "  + sv2.noOfInstances);

    StaticVariable sv3 = new StaticVariable();
    System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
    System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);
    System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);
  }
}

程序输出如下:

正如我们在这个例子中看到的,每个对象都有自己的类变量副本。

C:\java>java StaticVariable
No. of instances for sv1 : 1
No. of instances for sv1 : 2
No. of instances for st2 : 2
No. of instances for sv1 : 3
No. of instances for sv2 : 3
No. of instances for sv3 : 3

如果你将这个变量标记为static,那么你将需要静态方法来再次访问这些值,如果你已经考虑只在静态方法中使用这些变量,这将是有用的。如果是这样,那么这个就是最好的了。

但是,您现在可以将变量设为公共,因为没有人可以像“System”那样修改它。Out”,这取决于你的意图和你想要达到的目标。

根据Jon的回答,如果你使用静态韵母,它将表现为一种“定义”。一旦你编译了使用它的类,它将在编译后的.class文件中被烧毁。 在这里查看我的帖子。

对于你的主要目标:如果你没有在类的不同实例中使用不同的NUMBER,我建议使用final和static。 (您只需要记住,在不考虑像我的案例研究所描述的那样可能出现的问题的情况下,不要复制已编译的类文件。大多数情况下不会发生这种情况,别担心:))

为了向你展示如何在实例中使用不同的值,检查下面的代码:

public class JustFinalAttr {
  public final int Number;

  public JustFinalAttr(int a){
    Number=a;
  }
}

...System.out.println(new JustFinalAttr(4).Number);

静态意味着“与类相关联”;没有它,变量与类的每个实例相关联。如果它是静态的,这意味着内存中只有一个;如果没有,您将为创建的每个实例创建一个。静态意味着只要类被加载,变量就会保留在内存中;如果没有它,当变量的实例被回收时,变量就会被回收。

虽然其他答案似乎很清楚地表明,通常没有理由使用非静态常数,但我找不到任何人指出,可以在常量变量上使用不同值的各种实例。

考虑下面的例子:

public class TestClass {
    private final static double NUMBER = Math.random();

    public TestClass () {
        System.out.println(NUMBER);
    }
}

创建三个TestClass实例将打印三次相同的随机值,因为只生成一个值并将其存储到静态常量中。

但是,当尝试下面的例子时:

public class TestClass {
    private final double NUMBER = Math.random();

    public TestClass () {
        System.out.println(NUMBER);
    }
}

创建三个TestClass实例现在将打印三个不同的随机值,因为每个实例都有自己随机生成的常量值。

我想不出在任何情况下,在不同的实例上有不同的常量值是真正有用的,但我希望这有助于指出静态韵母和非静态韵母之间有明显的区别。