Java Constant
A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how to define them in this article.
Definition
A constant is a variable that, once assigned, cannot be altered. The constants are not directly supported by Java. Both non-access modifiers static as well as final can be used as a substitute when defining constants in Java.
Declaration of Constant in Java
In Java, the static and final modifiers are used to define any value as a constant. They are also referred to as non-access modifiers. The identifying name has to be in upper case letters in accordance with the Java naming convention.
Final and Static Modifiers
Utilizing the static modifier is done so that memory can be managed.
The variable can also be used without having to load any instances of the class in which it has been declared.
The final modifier indicates that the variable's value cannot be altered. Additionally, it declares the primitive data type unchangeable or immutable. The last modifier fixes the variable, making it accessible without the requirement to import an instance of its containing class due to the static modifier.
All instances of the class (within which the constant is declared) will be capable of accessing the variable and modify its value if it is declared as static. We leverage the final modifier together with a static modifier to solve this issue.
It is inappropriate to establish numerous instances with the same constant value for each particular object once the variable is identified as final.
The variable remains unchanged and only needs to be instantiated once when the static and final modifiers are used next to each other. Hence, we apply both the static and final modifiers to specify a variable to be a constant. All instances of its enclosing class are stored in the same shared memory address.
The purpose of Constants
Constants are used in programming to keep systems simple and comprehensible so that they can be widely interpreted by others. Even though a constant variable is memorized by both the JVM and the software, it also has an effect on performance.
Declaration Techniques
- Capitalize the title of the identifier that you intend to define as constant.
- If the private access-specifier is used just before constant name, the constant's value cannot be altered in that specific class.
- The constant's value can be modified in the program if the public access-specifier is used before its name.
Now, lets acknowledge ourselves the declaration of constants and usage of constants in specific circumstances. Go through the following programs to understand
In the following example, we have declared constant X as private. Inside the Demo() class, we have assigned in the constant X. After that, we have invoked the displayX() method.
File Name: Demo1.java
public class Demo1
{
private static final double X=75;
public static void main(String[] args)
{
System.out.println("Previous value of Dollar in Indian currency: "+X);
Demo d = new Demo();
d.displayX();
}
}
class Demo
{
private static final double X=79;
void displayX()
{
System.out.println("Current Value of Dollar in Indian currency: "+X);
}
}
Output
Previous value of Dollar in Indian currency: 75.0
Current Value of Dollar in Indian currency: 79.0
In the following example, we have declared constant L as public. Inside the display() method, we have assigned in the constant L. After that, we have invoked the display() method.
File name: Demo2.java
public class Demo2
{
//declaring L as constant
public static final double L= 10;
public static void main(String[] args)
{
display();
L = 20;
display();
}
void dsiplay()
{
System.out.print("The value of L cannot be changed " + L);
}
}
Output
Demo2.java:8: error: cannot assign a value to final variable X
L = 20;
A unique data type called an Enum permits a variable to be a collection of specified constants. A previously defined parameter for the variable must match the variable in question. Popular uses could include days of every week or compass headings.
File name: Enum2.java
public class Enum2
{
//defining the enum
public enum Languages {C,Python,Java,HTML,Javascript}
public static void main(String[] args)
{
//traversing the enum
for (Languages l : Languages.values())
System.out.println(l);
}
}
Output
C
Python
Java
HTML
Javascript