在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.

其他回答

Final:一旦Final变量被赋值,它总是包含相同的值。 无论变量是否是静态的 static:对于内存中一次初始化的所有实例,它将只有一个变量

从我所做的测试来看,静态最终变量与最终(非静态)变量是不一样的!最终(非静态)变量可以因对象而异!!但前提是在构造函数内部进行初始化!(如果它没有从构造函数初始化,那么它只是浪费内存,因为它为每个创建的不能更改的对象创建最终变量。)

例如:

class A
{
    final int f;
    static final int sf = 5;

    A(int num)
    {
        this.f = num;
    }

    void show()
    {
        System.out.printf("About Object: %s\n Final: %d\n Static Final: %d\n\n", this.toString(), this.f, sf);
    }

    public static void main(String[] args)
    {
        A ob1 = new A(14);
        ob1.show();

        A ob2 = new A(21);
        ob2.show();

    }
}

屏幕上显示的是:

关于对象:A@addbf1 最后:14 静态决赛:5分

关于对象:A@530daa 最后:21 静态决赛:5分

匿名的一年级IT学生,希腊

由于类中的变量在同一个命令中声明为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.

静态变量属于类(这意味着所有对象共享该变量)。非静态变量属于每个对象。

public class ExperimentFinal {

private final int a;
private static final int b = 999; 

public ExperimentFinal(int a) {
    super();
    this.a = a;
}
public int getA() {
    return a;
}
public int getB() {
    return b;
}
public void print(int a, int b) {
    System.out.println("final int: " + a + " \nstatic final int: " + b);
}
public static void main(String[] args) {
    ExperimentFinal test = new ExperimentFinal(9);
    test.print(test.getA(), test.getB());
} }

正如你可以看到上面的例子,对于“final int”,我们可以为类的每个实例(对象)分配变量,然而对于“static final int”,我们应该在类中分配一个变量(静态变量属于类)。