Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Reference Variable in Java
Next article icon

Reference Variable in Java

Last Updated : 06 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Before We Started with the Reference variable we should know about the following facts.

1. When we create an object (instance) of class then space is reserved in heap memory. Let's understand with the help of an example.

Demo D1 = new Demo();

Reference Variable

Now, The space in the heap Memory is created but the question is how to access that space?.  

Then, We create a Pointing element or simply called Reference variable which simply points out the Object(the created space in a Heap Memory).  

Heap Memory

Understanding Reference variable 

1. Reference variable is used to point object/values.

2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java.

3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value.

4. You can access object members using a reference variable using dot syntax.

<reference variable name >.<instance  variable_name / method_name>

Example:

Java
// Java program to demonstrate reference // variable in java  import java.io.*;  class Demo {     int x = 10;     int display()     {         System.out.println("x = " + x);         return 0;     } }  class Main {     public static void main(String[] args)     {         Demo D1 = new Demo(); // point 1          System.out.println(D1); // point 2          System.out.println(D1.display()); // point 3     } } 

 
 


Output
Demo@214c265e x = 10 0


 

Let us see what is actually happening step by step.


 

1. When we create an object of demo class new DEMO();, the default constructor is called and returns a reference of the object, and simply this reference will be stored to the reference variable D1 (As we know that associativity is Right-hand side to left-hand side).


 

2. The value of a reference variable is a reference. When we attempt to print the value of a reference variable, the output contains the name of the class which has been instantiated concatenated by @ and the hash code created for it by Java: the string Demo@214c265e tells us that the given variable is of type Name and its hexadecimal format of hash code is 214c265e.


 

3. At this point we will access the methods display() of the class demo using our custom reference variable that we created.


 

BINDING UP : The constructor call returns a value that is a reference to the newly-created object. The equality sign tells the program that the value of the right-hand side expression is to be copied as the value of the variable on the left-hand side. The reference to the newly-created object, returned by the constructor call, is copied as the value of the variable.

Java
import java.io.*; class Demo {     int x = 10;      int display()     {         System.out.println("x = " + x);         return 0;     } }  class Main {     public static void main(String[] args)     {           // create instance         Demo D1 = new Demo();          // accessing instance(object) variable         System.out.println(D1.x);          // point 3         // accessing instance(object) method         D1.display();     } } 

Output
10 x = 10


 

Reference accessing varibale

Java
// Accessing instance methods  import java.io.*; class Demo {        int x = 10;     int display()     {         System.out.println("x = " + x);         return 0;     } } class Main {     public static void main(String[] args)     {           // create instances         Demo D1 = new Demo();          Demo M1 = new Demo();          Demo Q1 = new Demo();     } } 

Methods of an instance

Java
// Pointing to same instance memory  import java.io.*; class Demo {     int x = 10;     int display()     {         System.out.println("x = " + x);         return 0;     } } class Main {     public static void main(String[] args)     {         // create instance         Demo D1 = new Demo();          // point to same reference         Demo G1 = D1;          Demo M1 = new Demo();          Demo Q1 = M1;          // updating the value of x using G!         // reference variable         G1.x = 25;          System.out.println(G1.x); // Point 1          System.out.println(D1.x); // Point 2     } } 

 
 


Output
25 25


 

Pointing to same memory

Note:

Here we pass G1 and Q1 reference variable point out the same object respectively. Secondly At Point 1 we try to get the value of the object with G1 reference variable which shows it as 25 and At Point 2 we try to get the value of an object with D1 reference variable which shows it as 25 as well. This will prove that the modification in the object can be done by using any reference variable but the condition is it should hold the same reference.  

More on Reference Variable

1. Reference Variable as Method Parameters:


 

As the value of a primitive variable is directly stored in the variable, whereas the value of a reference variable holds a reference to an object. We also mentioned that assigning a value with the equality sign copies the value (possibly of some variable) on the right-hand side and stores it as the value of the left-hand-side variable. A similar kind of copying occurs during a method call. Regardless of whether the variable is primitive or reference type, a copy of the value is passed to the method's argument and copied to that argument.


 

Note: Java only supports pass by value.

But we know that the reference variable holds the reference of an instance(OBJECT) so a copy of the reference is passed to the method's argument.  


 

Example:


 

Java
// Pass by reference and value  import java.io.*; class Demo {     int x = 10;     int y = 20;      int display(Demo A, Demo B)     {         //  Updating value using argument         A.x = 95;          System.out.println("x = " + x);          System.out.println("y = " + y);          return 0;     } } class Main {     public static void main(String[] args)     {         Demo C = new Demo();          Demo D = new Demo();          // updating value using primary reference         // variable         D.y = 55;          C.display(C, D); // POINT 1          D.display(C, D); // POINT 2     } } 

 
 


Output
x = 95 y = 20 x = 10 y = 55


 

SCENE 1 :


 

Changing Data in Heap Memory

SCENE 2:


 

Changing Data

Now, What is going on here, when we pass the reference to the method it will copy to the reference variable defined in the method signature and After that, they also have access to the object members. Here, We defined two instances named C and D. Afterwards we pass C and D to the method which further gives reference to A and B


 

At Point 1: A will update the value of x from 10 to 95, hence C.display() will show 95 20 but in another object D we update the value of x through D only from y =20 to 55, hence D, display() will show 10 and 55.


 

Note: Any Object Updation will not affect the other object's member.

2. What if we swap the reference variables with the help of the Swap Method?


 

The fact is if we try to swap the reference variable, then they just swap their Pointing element there is no effect on the address of reference variable and object(Instance) Space. Let's Understand It with the help of an example:
 


 

Java
// Swapping object references  import java.io.*; class Demo {      // Swapping Method     int Swap(Demo A, Demo B)     {         Demo temp = A;         A = B;         B = temp;         return 0;     } } class Main {     public static void main(String[] args)     {         Demo C = new Demo();          Demo D = new Demo();          // Passing C and reference variables         // to Swap method         C.Swap(C, D);     } } 

Here we created, two instances of demo class and passes it to swap method, further those C and D will copy their references to A and B respectively.Before swapping A point to C's(Object) and B point to D's(Object). After we perform swapping on A and B, A will now point D's(Object) and B will Point C's Object. As described in the figure.

Swapping references

 Note: There is no swapping between Variables, They only change their References.

3. What if we pass arrays to the method will it be able to update the Actual Array's values, even we know that a copy of the array is a pass to the formal Array?

The answer is YES, the values will be updated by Formal parameter, The Fact is, When we create an Array, a memory is assigned to the array of the desired size, and it returns the reference of the first array's element that is the base address that will store to the Formal Array(Method argument). As we learned earlier every pointing reference variable can change or update the object.

Arrays

Example:

Java
import java.io.*; class Demo {     int arrayUpdate(int[] formalArray)     {         formalArray[2] = 99;         formalArray[4] = 77;         return 0;     } } class Main {     public static void main(String[] args)     {         Demo d1 = new Demo();         int[] actualArray = { 1, 2, 3, 4, 5 };          for (int items : actualArray)             System.out.print(items                              + " , "); // printing array          System.out.println();         d1.arrayUpdate(actualArray);         System.out.println();          for (int items : actualArray)             System.out.print(items                              + " , "); // printing array     } } 

 
 


Output
1 , 2 , 3 , 4 , 5 ,   1 , 2 , 99 , 4 , 77 , 


 

4. this and super keywords are also Pointing Elements.


 

this keyword. In java, this is a reference variable that refers to the current object.


 

this in Java

super is used to refer immediate parent class instance variable. We can use the super keyword to access the data member or field of the parent class. It is used if parent class and child class have the same fields.


 

this and super

 5. null value of a reference variable.


 

Demo obj = null ;  

A. The null reference can be set as the value of any reference type variable.


 

B. The object whose name is obj is referred to by nobody. In other words, the object has become garbage. In the Java programming language, the programmer need not worry about the program's memory use. From time to time, the automatic garbage collector of the Java language cleans up the objects that have become garbage. If the garbage collection did not happen, the garbage objects would reserve a memory location until the end of the program execution.


 

Java
// null in java  import java.io.*; class Demo {     int x = 10;     int display()     {         System.out.println("x = " + x);         return 0;     } } class Main {     public static void main(String[] args)     {         Demo obj = null;          // accessing instance(object) method         Kuchbhi.display();     } } 

 
 


Output


 

Exception in thread "main" java.lang.NullPointerException at Main.main(File.java:17) Java Result: 1


 

Here, we try to access objects' members by a reference variable that is pointing nothing(null), and hence it shows NullPointerException. Now if you get the error, the first step is to look for variables whose value could be null. Fortunately, the error message is useful: it tells which row caused the error. Just try it out yourself!


 


Next Article
Reference Variable in Java

M

mohitvishwakarma
Improve
Article Tags :
  • Java
  • Java Programs
  • Java Quiz
Practice Tags :
  • Java

Similar Reads

    Can Two Variables Refer to the Same ArrayList in Java?
    ArrayList class in Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add to it. It is present in java.util package. Syntax: ArrayList<E> list = new ArrayList<>();An ArrayList in java can be instantiated once with the help
    2 min read
    Parameter Passing Techniques in Java with Examples
    There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the "caller function" and B is called the "called function or callee function". Also, the arguments wh
    4 min read
    Java Object Oriented Programming - Exercises
    Looking for Java OOP exercises to test and improve your object-oriented programming skills? Explore our topic-wise Java OOP practice exercises, featuring over 25 practice problems designed to help you master key OOP concepts such as encapsulation, inheritance, polymorphism, and abstraction. Java is
    15+ min read
    Getter and Setter in Java
    In Java, Getter and Setter are methods used to protect your data and make your code more secure. Getter and Setter make the programmer convenient in setting and getting the value for a particular data type. Getter in Java: Getter returns the value (accessors), it returns the value of data type int,
    3 min read
    Java Memory Management
    Java memory management is a fundamental concept that involves the automatic allocation and deallocation of objects, managed by the Java Virtual Machine (JVM). The JVM uses a garbage collector to automatically remove unused objects, freeing up memory in the background. This eliminates the need for de
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences