This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the reference itself, why it is impossible?  RSS feed

 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Pass-By-Value Please (<- link), we have this quoting:
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?
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It means you can only change the reference where you have the reference.
 
Tim Cooke
Marshal
Posts: 3853
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a bit of the context missing here. That statement is in the context of using the B reference that refers to the same object as A.
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.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thompson wrote:. . .
- Change that A remote. //How to do this?
. . .
Can't be done. You can change the state of a mutable reference type but you cannot change the actual reference remotely.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ....
 
Tim Cooke
Marshal
Posts: 3853
233
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key phrase here is "From b you cannot change a". Take the example code I had:

Both a and b refer to the same Cat. Consider what you can do from b:

In terms of the code: b knows nothing about a and cannot make it point to a different object or null.
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim explained it well, but in case it is helpful, attached is a graphic.

The B reference to the object is only pointing to the object, not to the A reference, so the B reference can't change the A reference.

Reference-to-oject.png
[Thumbnail for Reference-to-oject.png]
Reference to an Object
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, I got it.

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
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your code should have read:-That way you do not need the AnotherCat class. And you shouldn't use capital letters for variable names. What do you mean by A being remote in line 3? There is nothing remote about it.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thompson wrote:. . . Cannot point an object to null like that (I think the exception is a String, so it's like your first answer here.)
What does that mean? I don't think it is right, however.

You cannot point objects at anything; you can however point references at objects.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does that mean? I don't think it is right, however.

I mean pointing reference of object, like this:
Campbell Ritchie wrote:
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when a reference variable points to any object,
what thing you can do with that particular reference variable:

  • You can make it point to null
  • you can make it point to another object
  • you can up cast it(but that won't change anything with reference to the existing variable)


  • for example:

    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.




  •  
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!