在Java中,有什么区别:
private final static int NUMBER = 10;
and
private final int NUMBER = 10;
两者都是私有的和final的,不同的是静态属性。
更好的是什么?,为什么?
在Java中,有什么区别:
private final static int NUMBER = 10;
and
private final int NUMBER = 10;
两者都是私有的和final的,不同的是静态属性。
更好的是什么?,为什么?
当前回答
从我所做的测试来看,静态最终变量与最终(非静态)变量是不一样的!最终(非静态)变量可以因对象而异!!但前提是在构造函数内部进行初始化!(如果它没有从构造函数初始化,那么它只是浪费内存,因为它为每个创建的不能更改的对象创建最终变量。)
例如:
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学生,希腊
其他回答
根据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);
如果你使用static,变量的值将在你所有的实例中是相同的,如果在一个实例中改变了,其他实例也会改变。
"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.
静态变量在应用程序的整个生命周期中都保留在内存中,并在类加载期间初始化。每次构造一个新对象时,都会初始化一个非静态变量。通常更好的方法是:
private static final int NUMBER = 10;
为什么?这减少了每个实例的内存占用。它可能也有利于缓存命中。这是有意义的:静态应该用于在特定类型(类)的所有实例(也就是对象)之间共享的东西。
这可能会有所帮助
public class LengthDemo {
public static void main(String[] args) {
Rectangle box = new Rectangle();
System.out.println("Sending the value 10.0 "
+ "to the setLength method.");
box.setLength(10.0);
System.out.println("Done.");
}
}