Private Final Variable in Java

A variable which is declared as private is not accessible outside of the class. That means you cannot access that private variable using a reference variable after instantiating the class. You can also declare a variable as private final in Java, then not only it will be accessible outside of the class, you will not be able to change the value once it is initialized.

Characteristics of Private Final Variable

  • It is not accessible outside of the class because it is private
  • You can initialize it only once anywhere in your class because it is final
  • It is ideal for member variables such as identifier or id as they should not be accessible outside of class and are initialized only once
  • You don’t have to initialize a private final variable at the time of declaration
  • You cannot initialize the private final variable to null before the actual initialization
  • A private final variable can be accessed any number of times, it only gives error if you try to reinitialize

Initialize Private Final String Variable at the Time of Declaration

In this example, you can see that we have a private final String variable in class Alpha which being initialized at the time of declaration. Since, var is initialized at the time declaration itself it cannot be reinitialized anywhere else.

package com.techstackjournal;

class Alpha {

	private final String var = "1234";

	public void printVar() {
		System.out.println(var);
	}

}

public class Main {

	public static void main(String[] args) {

		Alpha alpha = new Alpha();
		alpha.printVar();

	}

}

Initialize a Private Final String Variable in a Constructor

In this example, we’ll look into an example where the private final variable gets initialized within a constructor. You can see that we have initialized name variable with null, but the we will not be able to initialize id variable with null as it is a final variable.

package com.techstackjournal;

class Employee {

	private final String id;
	private String name = null;
	
	public Employee(String id, String name) {
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return id + " " + name;
	}

}

public class Main {

	public static void main(String[] args) {

		Employee emp = new Employee("E001","John Doe");
		System.out.println(emp.toString());

	}

}

What happens if you do not initialize a private final variable?

In this example, we will see what happens if you declare a private final variable but not initialized anywhere. Here, we have a private final variable called id, but it is not initialized anywhere. This will result in a compilation error “variable id not initialized in the default constructor”.

package com.techstackjournal;

public class Employee {

	private final String id;
	private String name;
	
	@Override
	public String toString() {
		return id + " " + name;
	}

}

Output:

error: variable id not initialized in the default constructor
        private final String id;
                             ^

What happens if you initialize a private final variable multiple times?

In this example, we will see what will happen if we declare a variable as private final but initialize it more than once. If you declare a private final variable and initialize it in multiple locations, the behavior will similar to a regular final variable, that is, it throws a compile error “cannot assign a value to final variable”

package com.techstackjournal;

public class Employee {

	private final String id;
	private String name;
	
	public Employee(String id, String name) {
		this.id = id;
		this.name = name;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return id + " " + name;
	}

}

Output:

error: cannot assign a value to final variable id
                this.id = id;
                    ^

Leave a Comment