Reference Variable in Java

In this post, we will see about Reference variable in java.
[toc]

Reference variable

A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference variable, and if the variable holds primitive types(int, float, etc.), then it is called non-reference variables.

For example, If we create a variable to hold a String object, then it is called reference variable because String is a class. See an example.

String str = "Java2blog"; // str is reference variable
MyClass mCl = new MyClass(); // mCl is reference variable
int a = 10; // non-reference variable

ReferenceVariable

Reference variable basically points to an object stored into heap memory.

Note: Default value of a reference variable always a null.

We can create reference variable of either built-in class or user-defined class. Java provides hundreds of built-in classes like String, ArrayList, Arrays, Math, etc. We can create reference of these classes and call their member fields and methods. Apart from the built-in class object, we can create reference of our custom(user-defined) class. For example, we created a class MyClass and created a reference variable myClass to hold its object inside the Demo class.

class MyClass{

    void showMyName(String name) {
        System.out.println("Your name is "+name);
    }
}

public class Demo { 

    public static void main(String[] args){  
        // Creating reference to hold object of MyClass
        MyClass myClass = new MyClass();
        myClass.showMyName("Java2blog");
    }
}

Output:

Your name is Java2blog

Reference variable can be of many types based on their scope and accessibility. For example,

  1. Static reference variable
  2. Instance reference variable
  3. Local reference variable

Static Reference Variable

A variable defined using static keyword is called static variable. Static variable can be a reference or non-reference as well. In this example, we created two static reference variables and access them inside the main() method.

public class Demo { 
    // Static reference variables
    static String str;
    static String str2 = "Java2blog";

    public static void main(String[] args){  
        System.out.println("Value of str : "+str);
        System.out.println("Value of str2 : "+str2);
    }
}

Output

Value of str : null
Value of str2 : Java2blog

Instance Reference Variable

A variable which is not static and defined inside a class is called an instance reference variable. Since instance variable belongs to object, then we need to use reference variable to call instance variable.

public class Demo { 
    // instance reference variables
    String str;
    String str2 = "Java2blog";

    public static void main(String[] args){  
        Demo demo = new Demo(); 
        System.out.println("Value of str : "+demo.str);
        System.out.println("Value of str2 : "+demo.str2);
    }
}

Output

Value of str : null
Value of str2 : Java2blog

Local Reference Variable

Reference variable can be declared as local variable. For example, we created a reference of String class in main() method as local reference variable.

public class Demo { 

    public static void main(String[] args){  
        // Local reference variables
        String str2 = "Java2blog";
        System.out.println("Value of str2 : "+str2);
    }
}

Output:

Value of str2 : Java2blog

That’s all about Reference variable in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.