2

I knew that in Java class instances are passed by references. I was trying to swap two class instances but it didn't worked. What I don't understand if they are passed through references then the code should have worked. What am I missing here?

public class Swapper {

    public static void main(String[] args) {
        NormalClass na = new NormalClass(1235);
        NormalClass nb = new NormalClass(5468);
        System.out.println("Before: 1st Object -->");
        na.print_obj();
        System.out.println("Before: 2nd Object -->");
        nb.print_obj();


        NormalClass.swap(na,nb);


        System.out.println("After: 1st Object -->");
        na.print_obj();
        System.out.println("After: 2nd Object -->");
        nb.print_obj();

    }

}

class NormalClass{
    public int _ID;
    public static void swap(NormalClass a, NormalClass b){
        NormalClass temp;
        temp = a;
        a = b;
        b = temp;
    }
    public void print_obj(){
        System.out.println(this._ID);
    }
    NormalClass(int __id){
        this._ID = __id;
    }
}
bytestorm
  • 1,411
  • 3
  • 20
  • 36

2 Answers2

6

I knew that in Java class instances are passed by references

Firstly, you don't pass instances, but reference to them. And the references are passed by value.

In your method, you passed references to two instances, which created a copy of both of them. Then you swapped those copy of references. Since those are passed by value, it wouldn't affect the original references, and hence the behaviour. Just FYI, everything in Java is passed by value.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

You can't directly update a callers reference. To do that kind of swap, you can pass in an array. Also, I would override toString() for printing -

public int _ID;

public static void swap(NormalClass[] arr) {
    NormalClass temp = arr[0];
    arr[0] = arr[1];
    arr[1] = temp;
}

@Override
public String toString() {
    return String.valueOf(this._ID);
}

NormalClass(int __id) {
    this._ID = __id;
}

public static void main(String[] args) {
    NormalClass[] arr = new NormalClass[] { new NormalClass(1235),
            new NormalClass(5468) };
    System.out.println("Before: 1st Object -->");
    System.out.println(arr[0]);
    System.out.println("Before: 2nd Object -->");
    System.out.println(arr[1]);

    NormalClass.swap(arr);

    System.out.println("After: 1st Object -->");
    System.out.println(arr[0]);
    System.out.println("After: 2nd Object -->");
    System.out.println(arr[1]);
}

Output is

Before: 1st Object -->
1235
Before: 2nd Object -->
5468
After: 1st Object -->
5468
After: 2nd Object -->
1235
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I got the problem in my code... But what's happening in the code you gave? – bytestorm Aug 21 '14 at 18:48
  • @Bishal Arrays are also passed by value, but since they are Object(s) the value is a reference. Therefore, you can change the elements within the array without modifying the caller's reference. And that's why it works. – Elliott Frisch Aug 21 '14 at 18:49