Instance Variable as Final in Java
Last Updated :
25 Apr, 2025
As we all know, when the value of a variable varies from object to object, then that type of variable is known as an instance variable. The instance variable is declared inside a class but not within any method, constructor, or block.
If we don't initialize an instance variable, then the JVM automatically provides a default value according to the data type of that instance variable. But if we declare an instance variable as final, then we must take care of its initialization behavior.
Why Use Final Instance Variables?
Using final ensures that the variable’s value cannot be changed once it is assigned. There is no strict requirement to make the variable final. When it is clear that you explicitly intend never to change the variable, it is considered good practice to make it final. Sometimes we use final instance variables in the creation of immutable classes.
Important Points About Final Instance Variables
1. Initialization is Mandatory
If the instance variable is declared as final, then we must perform initialization explicitly. The JVM does not provide any default value for final instance variables.
Example: Missing Initialization
Java
// Java program to illustrate the behavior
// of final instance variable
class Test {
final int x; // Not initialized
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x); // Compilation error
}
}
Output:
error: variable x not initialized in the default constructor
2. Initialization at Declaration
We can initialize a final instance variable at the time of declaration.
Example:
Java
// Java program to illustrate that
// final instance variable can be initialized at declaration
class Test {
final int x = 10;
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
3. Initialization Inside an Instance Block
We can also initialize a final instance variable inside a non-static or instance block.
Example:
Java
// Java program to illustrate that
// final instance variable can be
// initialized in an instance block
class Test {
final int x;
{
x = 10;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
4. Initialization Inside the Constructor
We can initialize a final instance variable inside a constructor.
Example:
Java
// Java program to illustrate that
// final instance variable can be initialized in constructor
class Test {
final int x;
Test() {
x = 10;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
Invalid Ways to Initialize Final Instance Variables
1. Cannot Be Initialized in a Static Block
Since instance variables belong to objects and static blocks run before object creation, it is not possible to initialize a final instance variable in a static block.
Example:
Java
// Java program to illustrate that we can't initialize
// final instance variable in a static block
class Test {
final int x;
public static void main(String[] args) {
// Cannot assign to non-static
// final field from static context
x = 10;
Test t = new Test();
System.out.println(t.x);
}
}
Output:
error: non-static variable x cannot be referenced from a static context
2. Cannot Initialize in Method
Final instance variables cannot be assigned inside instance methods after object construction, because they must be assigned before the constructor finishes.
Example:
Java
// Java program to illustrate that we can't initialize
// final instance variable in a method
class Test {
final int x;
public void m() {
x = 10;
}
}
Output:
error: cannot assign a value to final variable x
Similar Reads
Instance Variable Hiding in Java
One should have a strong understanding of this keyword in inheritance in Java to be familiar with the concept. Instance variable hiding refers to a state when instance variables of the same name are present in superclass and subclass. Now if we try to access using subclass object then instance varia
2 min read
final variables in Java
In Java, we can use final keyword with variables, methods, and classes. When the final keyword is used with a variable of primitive data types such as int, float, etc), the value of the variable cannot be changed. Example 1: Usage of final with primitive datatype Java // Java Program to illustrate
2 min read
Final Static Variable in Java
When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any
3 min read
Final Local Variables in Java
In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u
3 min read
java.time.InstantSource Class in Java
The java.time.Instant class in Java basically represents a point in time. It is a precise moment on the timeline of the Unix epoch. The Unix epoch is the time 00:00:00 UTC on 1 January 1970. The Instant class can be used to represent timestamps, durations, and intervals. Instant values can be obtain
3 min read
java.time.Instant Class in Java
In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface. Declaration of the Instant Class public final class Instant extends Object implements Temporal, TemporalAdju
4 min read
Assigning values to static final variables in Java
Assigning values to static final variables in Java: In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.For example, follow
1 min read
Using final with Inheritance in Java
Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are
4 min read
Static and Non Static Blank Final Variables in Java
In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f
6 min read
Instance Control Flow in Java
This article will explain how Instance Control Flow takes place whenever objects are created. Instance Control Flow In Normal Class Whenever we are executing a java .class file, 1st Static Control Flow will be executed. In the Static Control Flow, if we are creating an object the following sequence
7 min read