Reference Variable

Java is a strongly typed language, it means before using any variable we have to declare it first. During compilation compiler check the data type of variable and allocate memory for that variable. All primitive data type's variable store value in that memory.


	    int a=10;

Memory:
+---------------+
| 10 |
+---------------+
a

As shown above value will be stored in allocated memory. All primitive data variable store value in same manner. Now for user defined data type way to store object is different. Java uses 'new' keyword to allocate memory at runtime.

For example if we have a class complex, then for creating object of complex class following statement has to be written.



Example :


 // file: complex.java 
// www.jgyan.com
class complex
{
private int real;
private int img;

public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
new complex(); // object will be created.
}
}

When above get executed, then required memory will be allocated to object of complex class. But we can't access or manipulate this object because there is no variable to hold this object. These type of objects are known as "Anonymous object"


In order to manipulate object or to operate on the object we will hold object into variable. These variables are Reference Variable. Reference variable are variable of class data type, in above example name of class is complex. So one reference variable will be declared of complex data type.

Syntax :

    complex var;

Reference variable now hold a reference to object and this variable can be used for accessing object's public data, for calling class member functions.



Example :


 // file : complex.java 
// www.jgyan.com
class complex
{
private int real;
private int img;

public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
compl=new complex();

}
}

When this program will execute 'new' keyword allocate memory for object and 'comp1' reference variable hold that object.




Reference variable don't store object, they actually store a reference where that object is stored. When a object is passed in method it is called as "call by reference". So if a method perform any changes in object, those changes will be reflected in original object.



Example :


 // file: complex.java
// www.jgyan.com
class complex
{
private int real;
private int img;

public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
compl=new complex();
comp1.set(20,30);
comp1.show();
change(comp1);
comp1.show();

}
static void change(complex a)
{
a.set(23,34);
}
}
/* Output:
real:20
img:30
real:23
img:34
*/

In above program we have created a complex object and we have set value by 20 and 30. When we printed comp1 object on the screen, values printed are 20 and 30. Then we passed object in the method change, which is changing the values by 23,34. When execution of change method is over, control again transferred to main method and show method is called. Show method print value on the screen 23,34.


Whatever changes done by change method also visible in main method.

Important ! In java all objects are passed by "call by reference" mechanism.