开发者

scjp passing object

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 h1 don't change after calling doStuff(h1)? As I understand reference to object should be passed, and in method it should be replaced to new object.


In this part

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

the variable hotel is a new local variable, that receives the reference value. This new local variable is loaded with a new reference to a new Hotel instance in the very first line and this new reference is returned.

The outer local variable h1 will not change.


main:h1 = 0x0000100                     (the old Hotel's address)
       |
     copying
       |
        ------->  doStuff:hotel = 0x0000100  (the method call)
                  doStuff:hotel = 0x0000200  (the new Hotel's address)
                              |
                          copying
                              |
main:h2 = 0x0000200 <---------


I would be a bit specific here: rather than saying a reference is passed, think of it as "reference being passed by value". So basically, the method receives a copy of the reference which points to the object in consideration. Both the references (the original h1 and the new hotel) point to the same object but are still different. In the method, you modify the "reference" and not the object referenced by it and hence the result.

A good read might be this one where the author uses code samples using different languages.


It's becouse the object is "passed by value, not by reference".

What is passed by value, is it's reference. So, at your eyes you think it's passed by reference.

So, to make it clear, when you pass an object to a method, a new "pointer reference" is made, and that is passed. So if you modify it, nothing happens.

EDIT: Here some code

Hotel h1 = new Hotel(100); // h1 holds a reference to a Hotel object in memory
System.out.print(h1.getRoomNr() + " ");
Hotel h2 = doStuff(h1); // when doStuff is called, a new reference pointing to the same object is made, but if you change it, nothing will happen

Take a look at Core Java. Best book ever!


A reference to the object is passed, and this reference is passed by value. This means that a copy of the reference is passed. You may modify the contents of the hotel, and these changes will be visible to the caller. But if you assign a new Hotel to the hotel argument, only the copy of the original reference will be changed. The original reference will still point to the original Hotel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜