What is Constant in Java?
Constants can be declared using Java's static and final keywords. The static keyword is used for memory management, and the final keyword signifies the property that the variable's value cannot be changed. It makes the primitive data types immutable. According to Java Constant in Java is a value that cannot be changed once assigned. Constants are not supported directly in Java. Instead, an alternative way to define a constant in Java is using the static and final keywords.
Syntax for Java Constants
Example:
In the above statement, the variable PI is declared as a float with the value 3.14. The value of this constant variable - PI cannot be changed during the entire program.
Static and Final Modifiers
- Static modifier- It allows the constant to be available without loading an instance of its declaration class. A static modifier is used for memory management as well.
- Final modifier- It makes the variable immutable. This means the value of the variable cannot be changed.
In the above example, the static keyword causes the PI variable to be available without creating an instance of the class where it is declared. On the other hand, the final keyword keeps the value of the PI variable immutable.
Note:
The static non-access modifier has no role in making the variable constant. Then why is it used?
The static keyword helps in achieving memory efficiency. Since the value of the final variable cannot be changed, there is no point in keeping a copy of the variable for each instance of the class where it is declared. Hence, the variable is associated with the class rather than an instance.
Why do we use Constants in Java?
The constants are used in Java to make the program easy to understand and readable. Since constant variables are cached by application and JVM, it improves the performance of an application by making it efficient.
Examples of Java Constants
How To Declare a Constant in Java?
The static and final modifiers are used to declare any variable in Java as constant. These modifiers are also known as non-access modifiers. According to Java naming convention, we must capitalize the identifier name associated with a constant variable in Java.
Example:
Output:
Explanation: In this example, the PI variable is declared as a constant with a value of 3.14. We have printed the value of the PI variable within its class.
Declaring Constant as Private
Using the private access modifier before the constant name makes it inaccessible from other classes. It means the value of the constant can be accessed from within the class only.
Example:
Output:
Explanation:
- In the program above, we have declared a private constant variable PI inside ClassA.
- When we try to access the private constant variable from another class (ClassB), it throws an error that is as expected.
Declaring Constant as Public
Using the public access modifier before the constant name makes it available anywhere in the program. It means the value of the constant can be accessed from any class or package.
Output:
Explanation:
- In this example, the PI is declared a public constant variable with a value of 3.14.
- This means the constant variable can be accessed anywhere in the program.
- In this example, ClassA contains the declaration of the constant variable, accessed from ClassB.
Constants using Enumeration
- The Enumeration is created using the enum keyword in Java.
- The Enumeration in Java is a list of named constants.
- It is a datatype that contains a fixed number of constants. It cannot be changed at runtime or dynamically.
- It is similar to that of the final variables in Java.
- It is used for defining class types in Java. Using enumeration in class, the class can have constructor, methods, and instance variables.
Example:
Output:
Explanation:
- In the example, we have declared an Enumeration Car using the enum keyword. It contains three constants: KIA, Nano, and Maruti.
- In the main method of the Main class, we have declared a Car variable c holding the Car.Nano value.
- We have printed the value of the variable c and used a switch-case statement using its value as well.
Object referenced by Constant in Java is Mutable
- The final keyword makes the value of primitive data types immutable.
- However, when a final variable references an object, the properties of the object can be changed.
- Constant variables in Java contain a pointer that points to an immutable location. This means the variable's pointer will not change its location.
- However, the object referenced through that pointer can be changed. This means the value of the reference object is mutable.
Example:
Output:
Explanation:
- In the example above, the final variable obj2 points to an object obj1, not a primitive data type.
- Although the object assigned to the obj2 variable cannot be changed, the properties of the object obj2 can be changed.
Conclusion
- Constant in Java is an entity that cannot be changed once assigned. Constant in Java makes the primitive data types immutable.
- Syntax: static final datatype identifier_name = value;
- The constants are used in Java to make the program easy to understand and readable. Since constant variables are cached by both JVM and the Application, it improves the performance of an application by making it efficient.
- Static modifier- It allows the constant variable to be used even without defining the instance of its class. Static modifiers are used for managing memory.
- Final modifier- It makes the variable final, which means unchangeable.
- According to Java naming convention, constant names in Java must be capitalized.
- Private constant variables are accessible from within the class only, and public constant variables are accessible from anywhere.
- If a final variable point to an object, the properties of the object can be changed. However, the final variable cannot be reassigned to a different object altogether.