在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的,不同的是静态属性。
更好的是什么?,为什么?
当前回答
这是另一个简单的例子来理解静态,静态final, final变量的用法。代码注释有适当的解释。
public class City {
// base price that is always same for all objects[For all cities].
private static double iphone_base_price = 10000;
// this is total price = iphone_base_price+iphone_diff;
private double iphone_citi_price;
// extra price added to iphone_base_price. It is constant per city. Every
// city has its own difference defined,
private final double iphone_diff;
private String cityName = "";
// static final will be accessible everywhere within the class but cant be
// changed once initialized.
private static final String countryName = "India";
public City(String cityName, double iphone_diff) {
super();
this.iphone_diff = iphone_diff;
iphone_citi_price = iphone_base_price + iphone_diff;
this.cityName = cityName;
}
/**
* get phone price
*
* @return
*/
private double getPrice() {
return iphone_citi_price;
}
/**
* Get city name
*
* @return
*/
private String getCityName() {
return cityName;
}
public static void main(String[] args) {
// 300 is the
City newyork = new City("Newyork", 300);
System.out.println(newyork.getPrice() + " " + newyork.getCityName());
City california = new City("California", 800);
System.out.println(california.getPrice() + " " + california.getCityName());
// We cant write below statement as a final variable can not be
// reassigned
// california.iphone_diff=1000; //************************
// base price is defined for a class and not per instances.
// For any number of object creation, static variable's value would be the same
// for all instances until and unless changed.
// Also it is accessible anywhere inside a class.
iphone_base_price = 9000;
City delhi = new City("delhi", 400);
System.out.println(delhi.getPrice() + " " + delhi.getCityName());
City moscow = new City("delhi", 500);
System.out.println(moscow.getPrice() + " " + moscow.getCityName());
// Here countryName is accessible as it is static but we can not change it as it is final as well.
//Something are meant to be accessible with no permission to modify it.
//Try un-commenting below statements
System.out.println(countryName);
// countryName="INDIA";
// System.out.println(countryName);
}
}
其他回答
非常少,而且是静态的
因为它们都是常数,所以区别不大。对于大多数类数据对象,静态意味着与类本身相关联的东西,无论用new创建了多少个对象,都只有一个副本。
因为它是一个常量,所以它实际上可能不会存储在类中或实例中,但是编译器仍然不会让你从静态方法访问实例对象,即使它知道它们会是什么。如果您不使反射API成为静态的,那么它的存在也可能需要一些无意义的工作。
读了答案后,我发现没有真正的测试能真正抓住重点。以下是我的观点:
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变量提供相同的地址。
从我所做的测试来看,静态最终变量与最终(非静态)变量是不一样的!最终(非静态)变量可以因对象而异!!但前提是在构造函数内部进行初始化!(如果它没有从构造函数初始化,那么它只是浪费内存,因为它为每个创建的不能更改的对象创建最终变量。)
例如:
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学生,希腊
"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 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实例现在将打印三个不同的随机值,因为每个实例都有自己随机生成的常量值。
我想不出在任何情况下,在不同的实例上有不同的常量值是真正有用的,但我希望这有助于指出静态韵母和非静态韵母之间有明显的区别。