Java pass by reference or pass by value

Java is pass by value and not pass by reference i.e. Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But if you change the reference inside method, original reference will not get change. If it was pass by reference, then it would have got changed also. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of custom types.Let’s discuss with one example as below.

public class Bar
{
    private String property;
 
    public Bar (String property){
        this.property = property;
    }
    public String getProperty() {
        return property;
    }
    public void setProperty(String property) {
        this.property = property;
    }
}
 
public class Main
{
     public static void main(String[] args){
          Bar bar = new Bar("bar");
          method1(bar); // here we are changing the instance that the reference variable "bar" refers
          method2(bar); // here we are not changing the reference!
     }
     public static void method1(Bar m1bar) {
          mbar.setProperty("modifybar");
   
     }
     public static void method2(Bar m2bar) {
         Bar newbar = new Bar("newbar");
         m2bar = newbar;
     }
}

In above example, method1() logical address bits of first instance(bar) are copied to another reference variable(m1bar), thus resulting both references to point a single memory location where actual object is stored. Remember, making another reference to null will not make first reference also null. But, in changing the property from either reference variable have impact seen in other reference also. But in method2() we are creating new reference “newbar” of same class i.e. new memory location of instance and after that change the value of the passing reference variable i.e m2bar assign to new memory location.

Let’s see step by step in java when we pass any reference of custom types as any method parameters always the memory address is copied to new reference variable. See in below picture:

1. Bar bar = new Bar(“bar”);
This statement will create an instance of class Bar, with “property” initialized to “bar”. The reference to this created instance is assigned to variable bar;

bar

2. define public static void method1(Bar m1bar)
In this line of execution then a reference of type Bar with a name m1bar is declared and it’s initially assigned to null.

m1bar

3. call of method1(m1bar)
This statement assign same memory location of object with both reference variables.

assign


4. mbar.setProperty(“modifybar”);
This statement change the value property of instance and it is change for both reference variables.

modify

5. in method2(m2bar) create new reference with other instance with property “newbar” as Bar

assign1

6. newbar = new Bar(“newbar”);

assign2


7. m2bar=newbar
Here, we have three reference variables bar, m2bar and newbar when statement executes, m2bar and newbar will point to same instance created inside the method. But bar is unchanged and it is continually pointing to instance, it was pointing originally.

Hope it help to clear you concept about pass by value in java.

Happy learning!!!.

 

Previous
Next