Programming Tutorials

Object Reference Variables in Java

By: aathishankaran in Java Tutorials on 2007-03-06  

In Java, an object reference variable is a variable that holds the memory address of an object. It does not actually hold the object itself, but instead points to the memory location where the object is stored.

Here's an example:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) { = name;
        this.age = age;

    public String getName() {
        return name;

    public int getAge() {
        return age;

public class Main {
    public static void main(String[] args) {
        Person person1 = new Person("John", 25);
        Person person2 = person1; // assigning person1 to person2

        System.out.println(person1.getName()); // prints "John"
        System.out.println(person2.getName()); // prints "John"

        person2.setName("Jane"); // changing person2's name
        System.out.println(person1.getName()); // prints "Jane"
        System.out.println(person2.getName()); // prints "Jane"

In this example, we have a Person class with a name and age, and a Main class that creates two Person objects, person1 and person2. When we assign person1 to person2, we are actually copying the memory address of the object that person1 points to. So both person1 and person2 now point to the same object in memory.

When we change the name of person2, we are actually changing the name of the object that both person1 and person2 point to. This is why when we print out the names of person1 and person2 after the change, they both have the value "Jane".

In summary, object reference variables in Java allow us to point to and manipulate objects in memory without actually holding the object itself.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)