final reference variable

A final variable can’t change the value during the execution of the program. We have seen how the final variable works with primitive data types. In this article, we will see how the reference variable works with the final keyword.

A reference variable stores the reference of an object. For example

ArrayList<String> list = new ArrayList<String>();

Here the list is the reference variable for the object of ArrayList. A reference variable is only way to access an object.

What is the final reference variable?

When we use the final keyword with reference variable it means we can’t reassign it to another reference or object. But you can change the value of members by using the setter methods. When we create a reference variable with final keyword it means we can’t use the “=” operator to reassign it. If you try to reassign the value it will throw compilation error.

final reference variable

Let us discuss it with an example. We will create a class Student having some member variable. 

public class Student 
{ 
	int rollNo;
	String className;
	String name;
	
    public int getRollNo() {
		return rollNo;
	}

	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

	public static void main(String args[]) 
    { 
		final Student object = new Student();
		object.setName("Ram");
		object.setRollNo(123);
		object.setClassName("MCA");
		
		Student object1 = new Student();
		object1.setName("Krishan");
		object1.setRollNo(456);
		object1.setClassName("MCA");
		
		// object is a reference variable with final keyword so we can't assign it
		object = object1;
    } 
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The final local variable object cannot be assigned. It must be blank and not using a compound assignment at ok.Student.main(Student.java:38)

In the above example, we are creating a reference with the final keyword of the Student class. In next line, we are trying to reassign it with another reference which is not possible. Because Java doesn’t permit to reassign the reference to the reference with the final keyword. But we can change the value of members by use of the setter method.

public class Student 
{ 
	int rollNo;
	String className;
	String name;
	
    public int getRollNo() {
		return rollNo;
	}

	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

	public static void main(String args[]) 
    { 
		final Student object = new Student();
		object.setName("Ram");
		object.setRollNo(123);
		object.setClassName("MCA");
		
		// object is a reference variable with final keyword
		// so we can change only its members value
		object.setName("Krishan");
		object.setRollNo(222);
		object.setClassName("MCA");
		
		
		System.out.println("Name :" +object.getName());
		System.out.println("Class name :" +object.getClassName());
		System.out.println("RollNo :" +object.getRollNo());
    } 
}

Output: Name :Krishan
Class name :MCA
RollNo :222

In the above example, we have created a reference variable with the final keyword. Although we know we can’t reassign it with any reference of the object but we can change the value of variables of a final reference. We can the properties of an object but can change the reference value.

2 thoughts on “final reference variable”

Leave a Comment