Introduction
The general meaning of constant is something that does not change. In Java, the variables are said to be constant if their values do not change throughout the execution of the program.
As per the accepted convention, all the constants in Java are represented by capital letters. This helps in identifying them easily.
In this article, we will learn everything about constants like:
- How to define constants.
- Types of constants.
- Uses of constants.
Read More, addition of two numbers in java
How to Define a Constant
In Java, a chain of keywords “static final” is used to create a global variable with an unchangeable value.
If we want a value that cannot be changed and remain the same for every instance of the class, we need to combine the keyword static and final to achieve this in Java. The static keyword means the value is the same for every instance of the class. The final keyword means that once the variable is assigned a value, it can never be changed.
The combination of the static final in Java is how we create a constant variable. It is to be understood that static and final are two different keywords in Java. They have different meanings. But when both are combined together, only then do we get a truly constant variable.
The constants in Java are also known as identifiers as they are unique and can be identified easily.
Using only static or only final keywords for the variables will not make it completely constant. We shall see the same in the example later.
Syntax:
static final data_type CONSTANT_IDENTIFIER = value;
Example:
static final int demoVar = 18;
The Static Keyword
In Java, a static member in a class simply means that it will be shared by all of the class's objects. For each instance of the class, a new variable will not be created. Being "static" in Java simply means that no object is required to access this field/method.
One may think that if a variable is static so every time we create an object of the class, a new copy of the variable will not be created, and therefore it can be said as a constant. However, this is not correct.
The problem is that although the new variable will not be created for every instance (object) of the class, if the value of the static variable is changed by one instance, the change is reflected for the other instance too. This is completely against the concept of constants, where we claim that the value of the identifier remains constant throughout the execution of the program.
Example:
public class CodingNinjas {
//we create a static global variable with value=100
static int demoVariable = 100;
public static void main(String args[]) {
//first instance of the class
CodingNinjas object1 = new CodingNinjas();
System.out.println("Value for the first instance:" + object1.demoVariable);
//object1 changes the value of demoVariable to 200
object1.demoVariable = 200;
CodingNinjas object2 = new CodingNinjas();
//Value accessed by the second object/instance is 200 and not 100.
System.out.println("Value for the second instance:" + object2.demoVariable);
}
}
Output:
Value for the first instance:100
Value for the second instance:200
Explanation:
In the above code, we observe that when the first object (object1) of the class changes the value of the static variable to 200, the value accessed by the second object(object2) is 200. This proves that only making a variable static would not make it a constant.
You can also read about the topic of Java Destructor, Duck Number in Java
The Final Keyword
If a variable is declared final in Java, its value cannot be changed further in the entire code. Changing the value of a final variable will throw an error. However, using only the final keyword will not make a variable constant as per the definition we studied earlier.
The reason is that multiple instances of the same constant value will be created for every different object every time we create a new object. This means that not directly but indirectly, we can change the value of a final variable by creating a new object every time.
Example:
public class CodingNinjas {
//the final variable
final int variable;
//constructor to initialise the final variable
public CodingNinjas(int val){
this.variable=val;
}
public static void main(String args[]) {
//first instance of the class
CodingNinjas object1 = new CodingNinjas(100);
System.out.println("Value of the final variable for the first instance:" + object1.variable);
//second instance of the class
CodingNinjas object2 = new CodingNinjas(200);
System.out.println("Value of the final variable for the second instance:" + object2.variable);
}
}
Output:
Value of the final variable for the first instance:100
Value of the final variable for the second instance:200
Explanation:
- In the above example, we have a global final variable "variable" which is initialised using the constructor.
- For the first object, we pass 100 in the parameter. Therefore, the final variable gets the value 100.
- For the second object, we pass 200 in the parameter. Therefore, the final variable gets the value 200.
- We can see that although the variable has been marked as final but different instances of the class can have different values of the variable.
Constant
Now that we have discussed the static and final keywords and also why they are individually not sufficient to create a constant, let's try to define a constant with the help of an example.
Example 1:
public class CodingNinjas {
static final int DEMO_VARIABLE = 100;
public static void main(String args[]) {
System.out.println("The value of the constant is: "+ DEMO_VARIABLE);
}
}
Output:
The value of the constant is: 100
Example 2:
public class CodingNinjas {
//creating a constant by using staic and final keywords
static final int DEMO_VARIABLE = 100;
public static void main(String args[]) {
//first instance of the class
CodingNinjas object1= new CodingNinjas();
System.out.println("Value for the first object is: "+object1.DEMO_VARIABLE);
object1.DEMO_VARIABLE= 150; //java: cannot assign a value to final variable DEMO_VARIABLE error
//second instance of the class
CodingNinjas object2= new CodingNinjas();
System.out.println("Value for the second object is: "+object2.DEMO_VARIABLE);
}
}
Output:
java: cannot assign a value to final variable DEMO_VARIABLE
Explanation:
- Since the identifier DEMO_VARIABLE is a constant when the first object tries to change the value of the identifier, the error is shown at the console.
Example:
public class CodingNinjas {
//creating a constant by using staic and final keywords
static final int DEMO_VARIABLE = 100;
public static void main(String args[]) {
//first instance of the class
CodingNinjas object1= new CodingNinjas();
System.out.println("Value for the first object is: "+object1.DEMO_VARIABLE);
//second instance of the class
CodingNinjas object2= new CodingNinjas();
System.out.println("Value for the second object is: "+object2.DEMO_VARIABLE);
}
}
Output:
Value for the first object is: 100
Value for the second object is: 100
Explanation:
- When we removed the line object1.DEMO_VARIABLE= 150; the error was removed. This is because now, no attempt was made to change the constant identifier.
- The value of DEMO_VARIABLE is 100 for both instances. This proves that DEMO_VARIABLE is a constant.
You can practice by yourself with the help of online Java Compiler.