Reference Variable in Java

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

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.

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.

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.

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.

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.

Output:

Value of str2 : Java2blog

That’s all about Reference variable in java.

Was this post helpful?

Related Posts

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusionWas this post helpful? An object in Java is an instance of a class. It is a […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in JavaWas this post helpful? In this post, we will see how […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusionWas this post helpful? In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classesWas this post helpful? In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusionWas this post helpful? In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusionWas this post helpful? If […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.