Final Static Variable in Java
Last Updated :
24 Apr, 2025
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
// 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);
}
}
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
// 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);
}
}
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
// 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:
Java
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);
}
}
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 company
Key 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.
Similar Reads
Static Variables in Java In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we u
3 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 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
Using Static Variables in Java Here we will discuss the static variables in java. Java actually doesnât have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of stati
3 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
Are Static Local Variables Allowed in Java? In Java, there are different types of variables, each with its own behavior and scope. Understanding these variables plays a very important role. In this article, we will discuss the concept of static with local variables, and we will also discuss in Java static local variables are allowed or not.Wh
3 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 Variable as Final in Java 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 automati
3 min read
Scope of Variables in Java The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about
7 min read
Environment Variables in Java In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
5 min read