Pass by Reference in Java Example

1. Introduction

In Java, understanding how arguments are passed to methods is crucial for effective programming. While Java is often said to be a "pass-by-value", this description can be confusing when it comes to objects. In reality, Java passes the reference to objects by value. This means that when you pass an object to a method, you pass the reference to it, not the object itself. However, because the reference is passed by value, you can't change the original reference that was passed in. This blog post will demonstrate how passing by reference works in Java through a simple example.

2. Program Steps

1. Create a class that contains a method to modify the properties of an object.

2. Demonstrate how changes to an object's properties within a method affect the original object.

3. Show that changing the reference to a new object within the method does not affect the original reference.

3. Code Program

class ExampleObject {
    public int value;

    public ExampleObject(int value) {
        this.value = value;
    }
}

public class PassingByReferenceExample {
    public static void main(String[] args) {
        ExampleObject obj = new ExampleObject(10);

        System.out.println("Before modification: " + obj.value);
        modifyObject(obj);
        System.out.println("After modification: " + obj.value);

        resetObject(obj);
        System.out.println("After reset: " + obj.value);
    }

    // Step 2: Modifies the object's property
    public static void modifyObject(ExampleObject exampleObj) {
        exampleObj.value = 20; // Changes the object's property
    }

    // Step 3: Attempts to reset the object by changing the reference
    public static void resetObject(ExampleObject exampleObj) {
        exampleObj = new ExampleObject(30); // This does not affect the original object
    }
}

Output:

Before modification: 10
After modification: 20
After reset: 20

Explanation:

1. The program starts with the ExampleObject class, which has a single property, value. This class is used to demonstrate the effects of modifying an object's properties and references within methods.

2. In the main method, an instance of ExampleObject is created with value initialized to 10. This object is then passed to the modifyObject method.

3. The modifyObject method receives the reference to the ExampleObject instance and modifies its value property to 20. Since the reference to the object is passed by value, changes to the object's properties within the method reflect on the original object.

4. The resetObject method is intended to reset the object by changing its reference to a new ExampleObject instance. However, because Java passes the reference by value, changing the reference inside the method does not affect the original reference outside the method. Therefore, the value property of the original object remains 20.

5. The output demonstrates that while you can modify the properties of the object passed to a method (since you're modifying the object through its reference), attempting to change the reference itself to point to a new object has no effect on the original reference outside the method. This illustrates the concept of passing the reference by value in Java.

Comments