Open In App

Static and Non Static Blank Final Variables in Java

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

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);  
    }
}

Output
10
20


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); 
    }
}

Output
1


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:

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() {} 
} 

Output
Logging in as guest
User ID: -1


Practice Tags :

Similar Reads