Blank Final Variable in Java

If you are a beginner? Then you must read the final variable first. Because in this article, we will discuss the Final variable in deep and detail. We will use the initialization of the final variable with a constructor that we have discussed in separate article final variables.   

Here is the table content of the article will we will cover this topic.
1. What is the Blank final variable in java?
2. RULES to initialize the Blank final variable in the constructor?
3. Real-life example?
4. Why we use the Blank final variable?

blank final variable in java

What is the Blank final variable in java?

As you already know, a final variable can be initialized only one time. You can’t initialize a final variable more than one time. Java provides three ways to initialize it.  But some time programmer doesn’t want to initialize a final variable at declaration time. So the Blank final variable is a final variable that is not initialized during declaration. It initializes by the constructor.

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

RULES to initialize the Blank final variable in the constructor

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

2.  If you have more than one constructor, then every constructor must be initialized the Blank final variable.

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

Real-life example

Suppose we want to maintain the data of persons. Each person has a name, address, PAN, etc. But a PAN can register for the only single person we don’t want to change during the execution of the program. So, we will create a Blank final variable so that value could be provided during the Construction of the Object. In this example, we will create a final variable PAN so that it will remain the same during the program execution.

public class Person
{
	String name;
	String address;
	final int PAN;
	
	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;
	}
	
	Person(int pan)
	{
		this.PAN = pan;
	}
	
	void showData(Person person)
	{
		System.out.println("Name of Person : " + person.getName());
		System.out.println("Address of person : " + person.getAddress());
		System.out.println("PAN of person : "+  person.PAN);
	}
	
	public static void main(String arg[])
	{
		Person person1 =  new Person(123456);
		person1.setName("Ram");
		person1.setAddress("India");
		person1.showData(person1);
		
		Person person2 =  new Person(654321);
		person2.setName("John");
		person2.setAddress("USA");
		person2.showData(person2);
	}
}

Output: Name of Person: Ram
Address of person: India
PAN of person: 123456
Name of Person: John
Address of person: USA
PAN of person: 654321

In the above example, we are creating a Blank final variable in the Person class. So whenever we create an object of a person, we can provide different values to the Final variable for each person. We are declaring final int PAN. For best practices, we should use the UPPERCASE letters for the final variables.

Why we use the Blank final variable?

Sometimes we don’t know the value of the final variable during the creation of the class. The value could be decided during the construction time of the object.
If we want to create a constant value that will be common for all the objects, then we should initialize the final variable during the declaration.
But there may be a situation when we want to create a constant value that will not be common for all objects. We want to make it constant only within the object. When a constructor will initialize the Blank final variable? Then it will create a new copy for each object.

Suppose you are providing login functionality on your website. A user can set the username only when he/she is creating the account on the website. After that, it should not be changed. Here you can create a blank final variable of username.
Each user will provide the username during the creation of the account.

public class Login
{
	final String USERNAME;
	String address;
	String name;
	
	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;
	}
	
	Login(String userName)
	{
		this.USERNAME = userName;
	}
	
	void showData(Login login)
	{
		System.out.println("Name of Person : " + login.getName());
		System.out.println("Address of person : " + login.getAddress());
		System.out.println("PAN of person : "+  login.USERNAME);
	}
	
	public static void main(String arg[])
	{
		Login user1 =  new Login("abc123");
		user1.setName("Ram");
		user1.setAddress("India");
		user1.showData(user1);
		
		Login user2 =  new Login("123abc");
		user2.setName("John");
		user2.setAddress("USA");
		user2.showData(user2);
	}
}

Output: Name of Person: Ram
Address of person: India
PAN of person: abc123
Name of Person: John
Address of person: USA
PAN of person: 123abc

Leave a Comment