You can change the object A refers to, but you can't take the A reference variable and do something to it -- like redirect it to reference a different object, or null.
Does it means that when I created an object, I cannot change the reference of that object point to the new object or even null?
Let's say I want to do this,
How can I do that, and if it is impossible, why?
Both a and b refer to the same Cat. From b you can change the Cat object that a also refers to, but you cannot change a and make it point to a different Cat or null.
Tim Cooke wrote:... but you cannot change a and make it point to a different Cat or null.
Yes, this is what I wanna ask, why I cannot change A and make it point to different Object (or null)?
Is it a convention or there is a reason behind it?
Please pardon me, I just want to understand ....
This is the key that I missed, and Tim has pointed it out,
Tim Cooke wrote:The key phrase here is "From b you cannot change a".
I cannot change A from B (in the context), but I still can change A outside B by create a statement
Again, thanks you guys very much!
Campbell Ritchie wrote:I think your code should have read:-
Ahh, this is more clear. Thanks!
What do you mean by A being remote in line 3? There is nothing remote about it.
Yes, you're right. Cannot point an object to null like that (I think the exception is a String, so it's like your first answer here.)
what thing you can do with that particular reference variable:
Here 'd' still refer to an object of Dog type
you can let the reference variable to point to the same object,to which it was pointing earlier already.
Hai Thompson wrote:Does it means that when I created an object, I cannot change the reference of that object point to the new object or even null?
Yes, that's precisely what it means; however, I think you may be confusing objects with variables.
When you create an object in Java (with new), what you get back is a reference to that object - which is kind of like its address in memory. You do NOT get back the object itself.
This is completely different to C++, which allows you to deal with objects in many ways: (a) directly, (b) as a memory address, (c) as a "reference" (which is not the same as a Java reference), or (d) as a "pointer to" the object, In Java there is only ONE way of handling an object: By its reference.
So, when you see the statement:
Dog d = new Dog();
what it's actually saying is: "Create a new Dog object, return its reference, and assign it to the variable 'd'".
'd' is therefore a reference to your new Dog object, NOT the object itself.
So, when we say that a reference cannot be changed, what we mean is that you cannot change the reference of the object you just created. However, 'd' is a variable, so you CAN change it to point to a different object, as in your Cat example above.
And just as a final point:
when we talk about "pass by value", what we mean is that a method receives a copy of the value in question, so if you have the following code:the assignment at line 2 changes n but NOT number because n is a copy of number.
Likewise, if you have:the assignment at line 2 changes s to point to a different String object ("Hello"), but does NOT change world because s is a copy of world (ie, a copy of the reference it holds).
Hope it helps.