简体   繁体   English

如何在 Java 中声明一个常量?

[英]How to declare a constant in Java?

We always write:我们总是这样写:

public static final int A = 0;  

Question:问题:

  1. Is static final the only way to declare a constant in a class? static final是在类中声明常量的唯一方法吗?
  2. If I write public final int A = 0;如果我写public final int A = 0; instead, is A still a constant or just an instance field ?相反, A仍然是一个常量还是只是一个实例字段
  3. What is an instance variable ?什么是实例变量 What's the difference between an instance variable and an instance field ?实例变量实例字段有什么区别?

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意味着无论创建多少个对象,变量都只有一个实例,并且该变量的值永远不会改变。

  1. You can use an enum type in Java 5 and onwards for the purpose you have described.您可以在 Java 5 及更高版本中使用enum类型来实现您所描述的目的。 It is type safe.它是类型安全的。
  2. A is an instance variable. A 是一个实例变量。 (If it has the static modifier, then it becomes a static variable.) Constants just means the value doesn't change. (如果它有 static 修饰符,那么它就变成了一个静态变量。)常量只是意味着值不会改变。
  3. Instance variables are data members belonging to the object and not the class.实例变量是属于对象而不是类的数据成员。 Instance variable = Instance field.实例变量 = 实例字段。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM