Java static method parameters

Posted by Blitzkr1eg on Stack Overflow See other posts from Stack Overflow or by Blitzkr1eg
Published on 2010-04-28T15:58:31Z Indexed on 2010/04/28 16:03 UTC
Read the original article Hit count: 208

Why does the following code return 100 100 1 1 1 and not 100 1 1 1 1 ?

public class Hotel {
private int roomNr;

public Hotel(int roomNr) {
    this.roomNr = roomNr;
}

public int getRoomNr() {
    return this.roomNr;
}

static Hotel doStuff(Hotel hotel) {
    hotel = new Hotel(1);
    return hotel;
}

public static void main(String args[]) {
    Hotel h1 = new Hotel(100);
    System.out.print(h1.getRoomNr() + " ");
    Hotel h2 = doStuff(h1);
    System.out.print(h1.getRoomNr() + " ");
    System.out.print(h2.getRoomNr() + " ");
    h1 = doStuff(h2);
    System.out.print(h1.getRoomNr() + " ");
    System.out.print(h2.getRoomNr() + " ");
}
}

Why does it appear to pass Hotel by-value to doStuff() ?

© Stack Overflow or respective owner

Related posts about java

Related posts about static