Java Reference Variable

We often are stuck at discussion about Objects and their references, it is known fact that unlike C or C++, java doesn’t allows variable access using pointers, well why it does not allows is itself a whole topic which you can read here. Today we will discuss about what all types can be given to java variables or objects. Before moving on to that concept let’s have a look at what do we mean by Reference Variable?

Java Reference Variable

What do we mean by Reference Variable?

A reference variable is used to access the object of a class. Reference variables are created at the program compilation time.

Example:

Primitive Variable vs Reference Variable

Having seen about reference variable above, let’s have basic understanding about the types of variables, i.e. Primitive Variables and Reference Variables. All the variables created using Primitive data types (char, int, boolean, float, double, short and long) are treated a bit differently by JVM in comparison to the ones which are used to point objects like the famous String class, File Objects, Thread objects and so on. If we talk about in terms of memory allocation, Reference Variables are the objects handles to the class references which are created in the Heap Memory.

Now let’s move on to discuss about Reference Variables.

Types of Java Reference Variable

Java has 4 types of Reference Variables available:

  1. Local Variable – Block Specific Or Method Specific Variables
  2. Static Variable – The class Variables
  3. Instance Variable – The Non Static Variables
  4. Method Parameter

Example of Reference Variable Being Created

In the below example we will take the case of an Employee class which has few primitive variables and we would take the reference of the Employee Class as “emp” and then explain its further working:

Now, we can create the reference variable of Employee class as:

This reference variable has nothing in it as it points to no physical location of main memory. If that’s complicating, in simple terms this reference object has no space being allocated in the memory and it is “null”.

Now if we print out this emp variable, it would result in an error if we don’t initialise the variable, if we set the object to null, it give output as “null”.

Now to make this reference actually work, for that the reference should have been allocated memory space. Below code helps us to achieve that:

Let’s understand this piece of code to understand how reference objects works, here new is Java keyword which is used to allocate memory to the Employee Object “emp”, this object is allocated memory based on the memory needed. The Employee() is the constructor of the Employee class which is used to assign any default values needed for the object created of the Employee class.

If the case above we reuse the “emp” reference to assign a new value we can do it by, simply calling the constructor with new keyword as under:

The above code would create a new reference. If you don’t want to allow references to be reassigned that can be done using the final keyword like below:

So, final is keyword which we use to define a variable which we don’t want to reassign.

Lets take an example program to understand reference varaible in Java.

The Output Of the above code would be something like:

Employee Id = 1

Employee 2

Also, please note that when we try to reassign the emp3 it results in compile time error.

Java Reference Variable

So, that was all about the study of reference variable in Java and stay tuned as we bring more interesting topics to study.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *