在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的,不同的是静态属性。
更好的是什么?,为什么?
当前回答
虽然其他答案似乎很清楚地表明,通常没有理由使用非静态常数,但我找不到任何人指出,可以在常量变量上使用不同值的各种实例。
考虑下面的例子:
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实例现在将打印三个不同的随机值,因为每个实例都有自己随机生成的常量值。
我想不出在任何情况下,在不同的实例上有不同的常量值是真正有用的,但我希望这有助于指出静态韵母和非静态韵母之间有明显的区别。
其他回答
读了答案后,我发现没有真正的测试能真正抓住重点。以下是我的观点:
public class ConstTest
{
private final int value = 10;
private static final int valueStatic = 20;
private final File valueObject = new File("");
private static final File valueObjectStatic = new File("");
public void printAddresses() {
System.out.println("final int address " +
ObjectUtils.identityToString(value));
System.out.println("final static int address " +
ObjectUtils.identityToString(valueStatic));
System.out.println("final file address " +
ObjectUtils.identityToString(valueObject));
System.out.println("final static file address " +
ObjectUtils.identityToString(valueObjectStatic));
}
public static void main(final String args[]) {
final ConstTest firstObj = new ConstTest();
final ConstTest sndObj = new ConstTest();
firstObj.printAdresses();
sndObj.printAdresses();
}
}
第一个对象的结果:
final int address java.lang.Integer@6d9efb05
final static int address java.lang.Integer@60723d7c
final file address java.io.File@6c22c95b
final static file address java.io.File@5fd1acd3
第二个对象的结果:
final int address java.lang.Integer@6d9efb05
final static int address java.lang.Integer@60723d7c
final file address java.io.File@3ea981ca
final static file address java.io.File@5fd1acd3
结论:
正如我认为java使原始类型和其他类型之间的区别。Java中的基本类型总是“缓存”的,对于字符串字面量也是如此(不是新的String对象),所以静态和非静态成员没有区别。
然而,如果非静态成员不是基元类型的实例,则存在内存复制。
将valueStatic的值更改为10甚至可以更进一步,因为Java将为两个int变量提供相同的地址。
正如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
Final:一旦Final变量被赋值,它总是包含相同的值。 无论变量是否是静态的 static:对于内存中一次初始化的所有实例,它将只有一个变量
假设类永远不会有多个实例,那么哪个实例占用更多内存:
私有静态最终int ID = 250; 或 private final int ID = 250;
我已经理解,静态将引用类类型,在内存中只有一个副本,而非静态将在每个实例变量的新内存位置。但是在内部,如果我们只是比较同一个类的1个实例(即不会创建多个实例),那么在1个静态final变量所使用的空间方面是否有任何开销?
通常,静态意味着“与类型本身相关联,而不是与类型的实例相关联”。
这意味着您可以在不创建类型实例的情况下引用静态变量,并且引用该变量的任何代码都引用完全相同的数据。将其与实例变量进行比较:在这种情况下,类的每个实例都有一个独立的变量版本。例如:
Test x = new Test();
Test y = new Test();
x.instanceVariable = 10;
y.instanceVariable = 20;
System.out.println(x.instanceVariable);
y. instancvariable和x. instancvariable是分开的,因为x和y指向不同的对象。
您可以通过引用引用静态成员,尽管这样做不是一个好主意。如果我们这样做:
Test x = new Test();
Test y = new Test();
x.staticVariable = 10;
y.staticVariable = 20;
System.out.println(x.staticVariable);
然后输出20个变量——只有一个变量,而不是每个实例一个变量。这样写会更清楚:
Test x = new Test();
Test y = new Test();
Test.staticVariable = 10;
Test.staticVariable = 20;
System.out.println(Test.staticVariable);
这使得这种行为更加明显。现代ide通常建议将第二个清单改为第三个清单。
没有理由像下面这样使用内联声明初始化值,因为每个实例都有自己的NUMBER,但总是具有相同的值(不可变并使用文字初始化)。这与所有实例只有一个最终静态变量是一样的。
private final int NUMBER = 10;
因此,如果它不能更改,就没有必要为每个实例提供一个副本。
但是,如果在这样的构造函数中初始化是有意义的:
// No initialization when is declared
private final int number;
public MyClass(int n) {
// The variable can be assigned in the constructor, but then
// not modified later.
number = n;
}
现在,对于MyClass的每个实例,我们可以有一个不同但不可变的number值。