Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a constant in Java?

Tags:

java

constants

We always write:

public static final int A = 0;   

Question:

  1. Is static final the only way to declare a constant in a class?
  2. If I write public final int A = 0; instead, is A still a constant or just an instance field?
  3. What is an instance variable? What's the difference between an instance variable and an instance field?
like image 208
Bin_Z Avatar asked Oct 09 '12 03:10

Bin_Z


People also ask

How do you declare a constant?

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.

What is declare constant variable in Java?

Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final .

How do you declare a constant in Java w3schools?

If a variable should have a fixed value that cannot be changed, you can use the const keyword. The const keyword declares the variable as "constant", which means that it is unchangeable and read-only.

How do you declare a class constant?

A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.


2 Answers

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

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.

like image 52
Peter Lundgren Avatar answered Oct 05 '22 23:10

Peter Lundgren


  1. You can use an enum type in Java 5 and onwards for the purpose you have described. It is type safe.
  2. A is an instance variable. (If it has the static modifier, then it becomes a static variable.) Constants just means the value doesn't change.
  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

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.

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); } 
like image 29
Oh Chin Boon Avatar answered Oct 05 '22 21:10

Oh Chin Boon