Static final variable in java

Before moving further, we recommend you to please read the static variable and final variable first. In this article, we will discuss how we can use the final keyword with a static variable. In this article, we will see how to initialize a static final variable in java. A variable can be static and final in java.

Here is the table content of the article we will cover this topic.
1. What is the static final variable in java?
2. How to declare a static final variable?
3. Initialization of static final variable?
i) initialization during declaration?
ii) initialization inside the static block?
4. What is the static blank final variable in java?
5. RULES to initialize the static blank final variable?
6. Real-life example?

java static final

What is the static final variable in java?

Most of us already know a static variable is declared are on class level. It means a static variable is shared with all the objects of the class. If we make any change in a static variable that reflects the other objects also. A static variable is always an instance variable of the class. You can’t declare a static variable within the method, block, or constructor. So, if we won’t initialize a static variable, then JVM will initialize it and provides the default value.

But if we are declaring a static variable with final keyword then JVM will not provide the default value. We must need to initialize a static final variable because JVM will not provide a default value to it.  A static final variable is a compile-time constant because it loads in memory when a class loads to memory.

How to declare a static final variable?

To declare a static final variable, we need to take care of two things, One declares with the final keyword and another is the initialization of the variable. It is not mandatory to initialize a static final variable at the time of declaration. We can initialize it in a static block also. If you are not initializing it at the time of declaration, it’s called a blank static final variable.

static final dataType variableName = value;

Here, static is a keyword that indicates the JVM, the variable will be shred for all objects.
final is a keyword that indicates the JVM to treat this variable as the final variable.
dataType is a type of variable which you want to create.
varaibleName is the name of the variable which you want to create.

class StaticFinalVariable 
{ 
    static final int a = 5;
    public void sum()
    {
    	a = a + 10;
    }
} 
public class MainClass 
{ 
    public static void main(String args[]) 
    { 
    	StaticFinalVariable object = new StaticFinalVariable();
        System.out.println("Value of static final variable = "+ StaticFinalVariable.a);
        
        
    } 
}

Output: Exception at compile time

Initialization of static final variable

In Java, the JVM especially takes care of the initialization of the static final variable. We must initialize the static final variable with a value, otherwise, the compiler will throw compile-time error. But as we have seen in the above example a static final variable can only be initialized once.

i) initialization during the declaration

We can initialize a static final variable during the declaration of the variable. As you know static variables are read before the class loading. So should initialize them class loading completion.

class StaticFinalVariable 
{ 
    static final int a = 5;
} 
public class MainClass 
{ 
    public static void main(String args[]) 
    { 
    	StaticFinalVariable object = new StaticFinalVariable();
        System.out.println("Value of static final variable = "+ StaticFinalVariable.a);
        
        
    } 
}

Output: Value of static final variable = 5

ii) initialization inside a static block

You can initialize a final variable in static block, but it should be static also. But static final variables can’t be assigned value in the constructor. So, they must be assigned a value with their declaration.

class StaticFinalVariable 
{ 
    static final int a;
    static
    {
    	a = 5;
    }
} 
public class MainClass 
{ 
    public static void main(String args[]) 
    { 
    	StaticFinalVariable object = new StaticFinalVariable();
        System.out.println("Value of static final variable = "+ StaticFinalVariable.a);
        
        
    } 
}

Output: Value of static final variable = 5

What is the static blank final variable in java?

As you already know a static final variable can be initialized only one time. You can’t initialize a static final variable more than one time. The blank static final variable is a final variable that is not initialized during declaration. It can be initialized in static block only.

A blank final variable can be initialized only in the static block. Java doesn’t provide any other way to initialize a blank static final variable.  

RULES to initialize the  static blank final variable

1. If you are not initializing a static final variable during the declaration(blank static final variable) then it must be initialized in a static block otherwise compiler throws an exception at compile time.

2. A static final blank variable can’t be initialized in constructor. It must be initialized in static block.

Real-life example

Suppose we want to maintain data of persons. Each person has a name, address, country name, etc. But we want to provide a common(static) and constant(final) country name country name to all person. Here we are creating a static final variable and we will be initialized it in static block. In this example, we will create a static final variable COUNTRYNAME so that it will remain the same during the execution of the program.

public class Person
{
    String name;
    String address;
    static final String COUNTRYNAME;
    
    static
    {
    	COUNTRYNAME = "India";
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    void showData(Person person)
    {
        System.out.println("Name of Person : " + person.getName());
        System.out.println("Address of person : " + person.getAddress());
        System.out.println("Country of person : "+  person.COUNTRYNAME);
    }
    
    public static void main(String arg[])
    {
        Person person1 =  new Person();
        person1.setName("Ram");
        person1.setAddress("India");
        person1.showData(person1);
        
        Person person2 =  new Person();
        person2.setName("John");
        person2.setAddress("USA");
        person2.showData(person2);
    }
}

Output: Name of Person : Ram
Address of person : India
Country of person : India
Name of Person : John
Address of person : USA
Country of person : India

3 thoughts on “Static final variable in java”

Leave a Comment