In Java programming, Java final keyword attains good importance as it can be applied in three places for three different entities – variables, methods and classes. In each place works differently and gives three different functionalities (outputs).
- A Java final variable cannot be reassigned.
- A Java final method cannot be overridden.
- A Java final class cannot be inherited.
In this tutorial, we are interested in the first entity – Java final variables.
Java does not support "const" (constant) keyword of C/C++. "const" is replaced with final in Java. A final variable cannot be reassigned. That is, once a value for a final variable is assigned, it cannot be changed in the remaining code.
Let us write a small code and explain and then further dig in the concept.
public class Student
{
public static void main(String args[])
{
int marks = 60; // marks is non-final and its value CAN be modified
System.out.println(marks); // prints 60
marks = 80; // now modified
System.out.println(marks); // prints 80 happily
final int age = 22; // age is final and CANNOT be modified
System.out.println(age); // prints 22
// age = 23; // error as age cannot be modified
}
}
By common sense, marks of a student can be changed but not his age. For this reason, age is declared as final.
Restrictions on the usage of final variables
Is it necessary to initialize a final variable or a final variable can be declared first and then assigned? Following examples give the answer.
1. Does the following code works?
public static void main(String args[])
{
final int marks; // declaration first
marks = 80; // then assignment
System.out.println(marks); // prints 80
}
The above code works fine and prints 80. A local final variable can be declared and assigned later. The reason is local variable, if not assigned, does not take a default value immediately. So, the local final variable can be given a value later after declaring it. But it is not possible with instance variable. See the next question.
2. Does the following code works?
public class Demo
{
final int marks;
public void display()
{
marks = 80;
}
}
Observe the above screenshot of compilation error. marks is instance variable not given a value at the time of declaration. In Java, if instance variables are not given a value, they take default values. For whole numbers, it is 0 value. So now, marks is given 0 value but here important is marks is declared final. When marks gets 0 value, it cannot be changed in the subsequent code.
3. Does the following code works?
In the above code, value for marks is assigned through a method call display() and is not working. But it is possible to assign through a constructor. See this code.
public class Student
{
final int marks;
public Student()
{
marks = 80;
}
public static void main(String args[])
{
Student std = new Student();
System.out.println(std.marks); // prints 80
}
}
In the above code, final marks is first declared and then assigned a value through constructor. It is working. As programmer, it is required to be aware of all these points.
We can derive two important rules from the above three questions.
- final local variables can be declared and then assigned (in two different statements).
- final instance variables should be assigned a value
- at the time of declaration itself
- through a constructor call
But not possible through a method call.
3. In the following code which is final – object marks array or values of marks elements like marks[0]?
public class Demo
{
public static void main(String args[])
{
final int marks[] = new int[1];
marks[0] = 60;
System.out.println(marks[0]); // prints 60
marks[0] = 80;
System.out.println(marks[0]); // element value is reassigned; prints 80
// marks = new int[1]; // raises compilation error
}
}
Here, final is object of marks which cannot be reassigned. But not values. It is shown in the above code.
Following is the screenshot when the comment for "// marks = new int[1];" is removed.
Storing a reference to object marks is final. That is, marks becomes immutable.
Some salient points to be noted still with final variables in a nutshell
(these points are important especially in interviews)
- final is the replacement of C/C++ "cosnt" keyword.
- "final" variable can be applied in three different contexts – variables, methods and classes.
- Common meaning of final is that something cannot be done.
- final variable can be assigned only once (cannot be reassigned).
- A blank variable forces the constructor to initialize with a value (but not possible with method).
- final, finally and finalize are very different.
- The variables declared within an interface are implicitly final.
- final serves code to be more clear (reader comes to a conclusion that the value never changes and is the same throughout the program) and helps in self-documenting the code. Code maintenance will be easier.
- JRE/JIT can make optimizations with final variables to improve performance.
- By C/C++ convention, Java Programmer writes final variables in uppercase (like predefined FlowLayout.LEFT, Thread.MIN_PRIORITY, Font.BOLD etc.).
- final is often used with static keyword for much more performance and easy in coding to call with class name(like predefined FlowLayout.LEFT, Thread.MIN_PRIORITY, Font.BOLD etc.).
- Examples where variable are to be declared final: PAN card number, identification marks, Adhar card number, Bank account number etc.
- In a programming language, instance variables (known as fields) denote the state of the object. If is safer to use final variables where Programmer need not think about the object state.
================Know more deep of final variables================
1. What is blank final variable with Example?
2. What is static blank final variable in Java with Example?