• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Changing the reference itself, why it is impossible?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76867
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. It means you can only change the reference where you have the reference.
 
Marshal
Posts: 5469
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5469
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




  •  
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic