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 change in that static variable reflects on the other objects operations. If we don’t initialize a static variable, then by default JVM will provide a default value for the static variable.
Final Static Variable
When we declare a static variable with a final modifier, it becomes a constant. We should take care of the following conventions:
- Declaring variables only as static can lead to changes in their values by one or more instances of a class in which it is declared.
- Declaring them as static final will help you to create a Constant. Only one copy of the variable exists, which can’t be reinitialized.
In short, we can say that,
- final: It cannot be reassigned once initialized.
- static: A single shared copy exists at the class level.
Important Points About final static Variable
1. Initialization is Mandatory
If the static variable is declared as final, then we have to perform initialization explicitly, whether we are using it or not, and the JVM won’t provide any default value for the final static variable.
Example: Initialization at the Time of Declaration
// Java program to illustrate that final
// static variable can be initialized
// at the time of declaration
class Test {
final static int x = 10;
public static void main(String[] args) {
System.out.println(x);
}
}
Output
10
2. Initialization Before Class Loading
For final static variable, it is compulsory that we should perform initialization before class loading completion. We can initialize a final static variable at the time of declaration.
Example: Inside a Static Block
// Java program to illustrate that final
// static variable can be initialized
// inside static block
class Test {
final static int x;
static {
x = 10;
}
public static void main(String[] args) {
System.out.println(x);
}
}
Output
10
3. Cannot Initialize in Non-static Methods or Blocks
We cannot initialize a final static variable inside a non-static method or block.
Example: Invalid Assignment Inside Method
// Java program to illustrate
// that we can't assign value
// to final static variable
// inside methods
class Test {
final static int x;
public static void m() {
}
public static void main(String[] args) {
System.out.println("Compile-time error if x is assigned in method.");
}
}
Output:
error: variable x not initialized in the default constructor Use Case Example
We can use a final static variable when we want to define constants like company name, which should not change:
class Geeks {
final static String company = "GFG";
String name;
int rollno;
public static void main(String[] args) {
Geeks ob = new Geeks();
ob.name = "Sweta";
ob.rollno = 7;
System.out.println(company);
System.out.println(ob.name);
System.out.println(ob.rollno);
}
}
Output
GFG Sweta 7
If we try to initialize a final static variable anywhere else then we will get compile time error,
cannot assign a value to final variable companyKey Points:
- static final is used to create constants in Java.
- It must be explicitly initialized.
- This can only be initialized during declaration or in a static block.
- This cannot be reassigned or initialized in methods.