Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

In Java, when you pass an object as an argument to a method, you are actually passing a reference to the object, not the object itself. This means that the method can modify the original object's state through the reference.

In Java, when you pass a variable as an argument to a method, the mechanism used for passing depends on the type of the variable. Java uses a combination of pass-by-value and pass-by-reference mechanisms.

  • Pass-by-Value:
    Java is primarily a pass-by-value language, which means that a copy of the value of a variable is passed to a method when you call it. Any changes made to the parameter inside the method do not affect the original variable outside the method.

Example:

public class Main {
    public static void main(String[] args) {
        int num = 5;
        System.out.println("Before calling the method: " + num);
        modifyValue(num);
        System.out.println("After calling the method: " + num);
    }
    
    public static void modifyValue(int value) {
        value = 10;
        System.out.println("Inside the method: " + value);
    }
}

Output:

Before calling the method: 5
Inside the method: 10
After calling the method: 5

In the example above, the value of num remains unchanged after calling the modifyValue method because it was passed by value.

  • Pass-by-Reference (for Objects):
    In Java, when you pass an object (reference type) as an argument to a method, the reference to the object is passed by value. This means that a copy of the reference is passed, but both the original and the copy refer to the same object in memory. If you modify the object's state inside the method, the changes are visible outside the method.

Example:

public class Main {
    public static void main(String[] args) {
        StringBuilder name = new StringBuilder("John");
        System.out.println("Before calling the method: " + name);
        modifyReference(name);
        System.out.println("After calling the method: " + name);
    }
    
    public static void modifyReference(StringBuilder value) {
        value.append(" Doe");
        System.out.println("Inside the method: " + value);
    }
}

Output:

Before calling the method: John
Inside the method: John Doe
After calling the method: John Doe

In the example above, the modifyReference method appends " Doe" to the name object, and this change is visible outside the method as well.

It's important to note that even though the reference is passed by value, you cannot reassign the reference to a new object inside the method and have it affect the original reference outside the method. In other words, you cannot change which object the original reference points to using pass-by-reference semantics.



InventoryItem Class - Previous example - Passing on an object as an argument

package OverloadConstructors;

/**
 * This program passes an object as an argument.
 * Book Example 6-13
 * Modified by Dr. Roark
 */

public class InventoryDemoTwo
{
    public static void main(String[] args)
    {
        // Create an InventoryItem object.
        InventoryItem item = new InventoryItem("Wrench", 20);
        // Pass the object to the DisplayItem method.
        System.out.println("The contents of item are:");
        displayItem(item);

        // Create another InventoryItem object.
        InventoryItem itemTwo = new InventoryItem("Wrench", 20);
        // Display the object's contents.
        System.out.println("The contents of item are:");
        displayItem(itemTwo);

        // Pass the object to the ChangeItem method.
        changeItem(itemTwo);
        // Display the object's contents again.
        System.out.println("Now the contents of item are:");
         displayItem(itemTwo);
    } //end of main

    /**
     * The following method accepts an InventoryItem
     * object as an argument and displays its contents.
     */
    public static void displayItem(InventoryItem pItem)
    {
        System.out.println("Description: " + pItem.getDescription());
        System.out.println("Units: " + pItem.getUnits());
    }

    /**
     * The following method accepts an InventoryItem
     * object as an argument and changes its contents.
     */
    public static void changeItem(InventoryItem pItem)
    {
        pItem.setDescription("Hammer");
        pItem.setUnits(5);
    }
}

This Java program is demonstrating how objects can be passed to methods as arguments and how a method can change the state of an object passed to it.

  1. main method first creates an InventoryItem object item with a description of "Wrench" and 20 units.

  2. Then, item is passed to the displayItem method, which accepts an InventoryItem object as an argument. The displayItem method prints the description and units of the InventoryItem passed to it.

  3. After that, another InventoryItem object itemTwo is created with the same values as item. It prints the description and units of itemTwo directly in the main method.

  4. Next, itemTwo is passed to the changeItem method, which also accepts an InventoryItem object as an argument. This method modifies the InventoryItem it receives by setting the description to "Hammer" and the units to 5.

  5. Finally, it again prints the description and units of itemTwo. However, this time the values are different because they were modified by the changeItem method.

This demonstrates that when you pass an object to a method, the method has access to the original object and can modify its state. This is different from passing primitive types to methods, where the method only gets a copy of the value and cannot modify the original.

  • No labels