Instance Variable vs Reference Variable in Java

In this post we’ll discuss the differences between Instance Variable and Reference Variable in Java, understand them with an example.

In a nutshell, a reference variable is any variable that can hold reference of an object and an instance variable is a primitive or non-primitive variable that is declared within a class.

Instance VariableReference Variable
Declared within a classCan be declared within a class, constructor, method and block of code as required
It belongs to an instance or objectIt also can be belong to an instance or object if it is declared within a class. It can be a local variable also
It can be a reference variable (non-primitive variable) or primitive variableOnly non-primitive variables are called as reference variable. It will also be a instance variable if it is declared within the class
It cannot be declared as staticIt can be declared as static. A static reference variable declared within class will be called as class variable
We can define access for instance variables using access modifiers like private, public, protected and defaultA reference variable declared within a class can be defined using access modifiers, as it will be a instance variable in that case. If a reference variable is within a code block then it cannot have access modifiers
Instance variables live till the object is aliveIf a reference variable is also an instance variable then it will live till object is alive. If the reference variable is defined within a code block like constructor or method, these variables live only during the execution of the block

Now, let us look at an example code and find which variable is what.

package com.techstackjournal;

class Employee {

	private String employeeId;
	private String name;
	private double salary;

	public Employee(String eId, String nm, double sal) {
		this.employeeId = eId;
		this.name = nm;
		this.salary = sal;
	}

}

public class InstanceAndRefDemo {

	public static void main(String[] args) {
		Employee emp = new Employee("E001", "John Doe", 12000);
	}

}
  • In the above example, Variables employeeId, name and salary variables are instance variables. That is because, they are declared directly within the class.
  • Variables employeeId, name are both instance variables and reference variables. That is because both these variables are declared within class, so they are instance variables, and both these variables are of a class type, in our case String, that is why it is also called as reference variables.
  • Variable salary is an instance variable but it cannot hold reference of an object. It is a primitive variable which holds a primitive value directly.
  • Variables eId, nm and emp are reference variables and also local variables. They are reference variables because they are of a class type, in our case String. The are also local variables because eId, nm variables are defined as constructor arguments. The scope of these arguments is limited to the constructor, means, its scope is local to the constructor. Similarly emp variable is also a class type, so it is a reference variable and it is declared within a method, so it is also called as local variable.

Leave a Comment