Static and Non Static Blank Final Variables in Java
Last Updated :
30 Apr, 2025
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 final variable. This article explains the key differences between static and non-static blank final variables with examples.
Types of Class Variables
There are two types of class-level variables in Java:
1. Instance variables: Instance variables in Java are variables which are declared within a class but outside any constructor, method, or block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables are created when an object is created with the use of the new keyword and destroyed when the object is destroyed. Instance variables are unique to each object, it simply means these variables can only be accessed through an object reference.
2. Static variables: Static variables are also known as class variables. Static variables are the variables in Java, that are declared with the help of the static keyword within a class, but outside a method, constructor, or block. There is only one copy of each static variable, regardless of how many objects are created from that class. Static variables are shared by all objects of the class.
Example:
Java
// Java program to illustrate
// static blank final variable
public class Geeks {
// Static variable
static String companyName = "GeeksForGeeks";
// Instance variable
int salary;
public Geeks(int salary) {
this.salary = salary;
}
public static void main(String[] args) {
Geeks s = new Geeks(100000);
Geeks g = new Geeks(200000);
System.out.println("Name: Shubham");
System.out.println("Company: " + companyName);
System.out.println("Salary: " + s.salary);
System.out.println("Name: Chirag");
System.out.println("Company: " + companyName);
System.out.println("Salary: " + g.salary);
// Changing the static variable
companyName = "Google";
System.out.println("\nAfter change");
System.out.println("Name: Shubham");
// Affects all objects
System.out.println("Company: " + companyName);
System.out.println("Salary: " + s.salary);
System.out.println("Name: Chirag");
// Affects all objects
System.out.println("Company: " + companyName);
System.out.println("Salary: " + g.salary);
}
}
Output:

Explanation: Here, in the above example, we have declared static variable companyName, when the compayName is updated, it affects all the objects s and g, we have also declared a instance variable salary, modifying the salary of one object s is not going to affect the salary of another object g, as instance variable are unique to each object.
Blank Final Variables
In Java, final variables are a type of variable whose value cannot be changed once they are assigned. However, variables that are declared but not initialized immediately are known as blank final variables.
There are two types of blank final variables:
- Static blank final variable
- non-static blank final variable
Now, we are going to discuss the Static and non-static blank final variables in detail.
Non-Static Blank Final Variable
A non-static blank final variable is an instance-level final variable that must be initialized inside the constructor of the class.
Key features of non static blank final variable:
- Each object gets its own final variable.
- It must be initialized in every constructor.
- Once assigned, cannot be changed.
Example:
Java
// Java program to demonstrate non static blank final variable
public class Geeks {
// Instance blank final variable
private final int b;
// Constructor to initialize the final variable
Geeks(int c) {
b = c;
}
public static void main(String[] args) {
Geeks g1 = new Geeks(10);
Geeks g2 = new Geeks(20);
System.out.println(g1.b);
System.out.println(g2.b);
}
}
Static Blank Final Variable
Static blank final variable is a variable in Java. It is shared among all the objects of a class, it simply means there is only one copy of it for the entire, class. The static keyword means that it belongs to the class, not to any specific object, and the final keyword means once the value is set it cannot be changed throughout the program.
Key features of static blank final variable:
- It can be initialized in a static block if it is not initialized during declaration.
- It must be initialized before any use. Otherwise, it will cause compile-time error.
- The value of a static final variable cannot be changed once it is initialized.
- We can access the static variable without creating the object, as it belongs to the class.
Example:
Java
// Java program to demonstrate static blank final variable
public class Geeks {
// Static blank final variable
private static final int a;
static {
a = 1;
}
public static void main(String[] args) {
System.out.println(Geeks.a);
}
}
Example: With try-catch
Java
// Java program to illustrate
// static blank final variable
public class UserLogin {
public static final long GUEST_ID = -1;
private static final long USER_ID;
static
{
try {
USER_ID = getID();
}
catch (IdNotFound e) {
USER_ID = GUEST_ID;
System.out.println("Logging in as guest");
}
}
private static long getID()
throws IdNotFound
{
throw new IdNotFound();
}
public static void main(String[] args)
{
System.out.println("User ID: " + USER_ID);
}
}
class IdNotFound extends Exception {
IdNotFound() {}
}
Output:

Explanation: The USER_ID field is a static blank final. It is clear that the exception can be thrown in the try block only if the assignment to USER_ID fails, so it is perfectly safe to assign to USER_ID in the catch block. Any execution of the static initializer block will cause exactly one assignment to USER_ID, which is just what is required for blank finals. But this program fails because, A blank final field can be assigned only at points in the program where it is definitely unassigned.
Here, compiler is not sure whether its been assigned in try block or not, so the program doesn’t compile. We can solve this by removing the static block and initializing the USER_ID at the time of declaration.
Example:
Java
// Java program to illustrate
// static blank final variable
public class UserLogin {
public static final long GUEST_ID = -1;
private static final long USER_ID = getUserIdOrGuest();
private static long getUserIdOrGuest()
{
try {
return getID();
}
catch (IdNotFound e) {
System.out.println("Logging in as guest");
return GUEST_ID;
}
}
private static long getID()
throws IdNotFound
{
throw new IdNotFound();
}
public static void main(String[] args)
{
System.out.println("User ID: " + USER_ID);
}
}
class IdNotFound extends Exception {
IdNotFound() {}
}
OutputLogging in as guest
User ID: -1
Similar Reads
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
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is
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
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
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. W
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
Why non-static variable cannot be referenced from a static method in Java
Java is one of the most popular and widely used programming language and platform. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In java, methods can be declared as either static or non-static.
4 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
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access
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 automat
3 min read