How to declare a constant in Java

We always write:

public static final int A = 0; 

Question:

  • Is static final only way to declare a constant in class?
  • If I write public final int A = 0; instead, A still a constant or just an instance field ?
  • What is an instance variable ? What is the difference between an instance variable and an instance field?
+58
java constants
Oct 09 '12 at 3:00
source share
3 answers
  • You can use the enum type in Java 5 onwards for the purpose described. This is a safe type.
  • A is an instance variable. (If it has a static modifier, it becomes a static variable.) Constants simply mean that the value does not change.
  • Instance variables are data members that belong to an object, not a class. Instance variable = instance field.

If you are talking about the difference between an instance variable and a class variable, an instance variable exists for every object created. Although the class variable has only one copy for each class loader, regardless of the number of objects created.

Java 5 and enum type

 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 want to change the value of the created enumeration, specify the mutator method.

 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; } } 

Access Example:

 public static void main(String args[]){ System.out.println(Color.RED.getColor()); // or System.out.println(Color.GREEN); } 
+37
Oct 09
source share

final means that after initialization, the value cannot be changed, which makes it a constant. static means that instead of the space allocated for the field in each object, only one instance is created for the class.

So, static final means only one instance of a variable, no matter how many objects are created, and the value of this variable will never change.

+62
09 Oct :
source share

Everything that is static is at the class level. You do not need to create an instance to access static fields / method. A static variable will be created once when the class is loaded. Instance variables are a variable associated with an object. This means that instance variables are created for every object you create. All objects will have a separate copy of the instance variable for themselves. In your case, when you declared it as static final , this is only one copy of the variable. If you change it from multiple instances, the same variable will be updated (however, you have a final variable, so it cannot be updated). In the second case, final int a also a constant, however, it is created every time you create an instance of the class in which this variable is declared.

Check out this Java tutorial for a better understanding.

+4
Oct 09
source share



All Articles