[英]How to declare a constant in Java?
We always write:我们总是这样写:
public static final int A = 0;
Question:问题:
static final
the only way to declare a constant in a class? static final
是在类中声明常量的唯一方法吗?public final int A = 0;
public final int A = 0;
instead, is A
still a constant or just an instance field ?A
仍然是一个常量还是只是一个实例字段?final
means that the value cannot be changed after initialization, that's what makes it a constant. final
意味着该值在初始化后不能更改,这就是使它成为常量的原因。 static
means that instead of having space allocated for the field in each object, only one instance is created for the class. static
意味着不是为每个对象中的字段分配空间,而是只为类创建一个实例。
So, static final
means only one instance of the variable no matter how many objects are created and the value of that variable can never change.因此,
static final
意味着无论创建多少个对象,变量都只有一个实例,并且该变量的值永远不会改变。
enum
type in Java 5 and onwards for the purpose you have described.enum
类型来实现您所描述的目的。 It is type safe. If you are talking about the difference between instance variable and class variable, instance variable exist per object created.如果您在谈论实例变量和类变量之间的区别,则每个创建的对象都存在实例变量。 While class variable has only one copy per class loader regardless of the number of objects created.
而不管创建的对象数量如何,类变量每个类加载器只有一个副本。
Java 5 and up enum
type Java 5 及更高版本的
enum
类型
public enum Color{
RED("Red"), GREEN("Green");
private Color(String color){
this.color = color;
}
private String color;
public String getColor(){
return this.color;
}
public String toString(){
return this.color;
}
}
If you wish to change the value of the enum you have created, provide a mutator method.如果您希望更改您创建的枚举的值,请提供一个 mutator 方法。
public enum Color{
RED("Red"), GREEN("Green");
private Color(String color){
this.color = color;
}
private String color;
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color = color;
}
public String toString(){
return this.color;
}
}
Example of accessing:访问示例:
public static void main(String args[]){
System.out.println(Color.RED.getColor());
// or
System.out.println(Color.GREEN);
}
Anything that is static
is in the class level.任何
static
东西都在类级别。 You don't have to create instance to access static fields/method.您不必创建实例来访问静态字段/方法。 Static variable will be created once when class is loaded.
静态变量将在类加载时创建一次。
Instance variables are the variable associated with the object which means that instance variables are created for each object you create.实例变量是与对象关联的变量,这意味着为您创建的每个对象创建实例变量。 All objects will have separate copy of instance variable for themselves.
所有对象都将拥有单独的实例变量副本。
In your case, when you declared it as static final
, that is only one copy of variable.在您的情况下,当您将其声明为
static final
,那只是变量的一个副本。 If you change it from multiple instance, the same variable would be updated (however, you have final
variable so it cannot be updated).如果您从多个实例更改它,则将更新相同的变量(但是,您有
final
变量,因此无法更新)。
In second case, the final int a
is also constant , however it is created every time you create an instance of the class where that variable is declared.在第二种情况下,
final int a
也是 constant ,但是每次创建声明该变量的类的实例时都会创建它。
Have a look on this Java tutorial for better understanding ,看一下这个 Java 教程以更好地理解,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.