• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object self-replacement  RSS feed

 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why this line doesn't work?



As I understand it, 'this' provides a reference to the current object. How do I reassign it from within the object itself? You can do it from outside just fine...



But if I do it from within the object, I get "The left-hand side of an assignment must be a variable".
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not possible to reassign an object from inside itself. You can't write this = . . .
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response.

I understand I can't do it. My IDE tells me that "The left-hand side of an assignment must be a variable" and it won't compile.

I'm trying to understand why, and also to figure out what I can do instead.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference variable like myObject lives anywhere in the software and points to an object that lives anywhere on the heap. this just has a special meaning. As soon as you are in the context of an object - like a non-static method - this points to the object that context belongs to. As you can't change the context you can't change the this reference.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object is what it is. You can't change that. As for what you can do instead, that depends on what you meant to do by assigning a value to "this". Perhaps you could explain that.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About what to do instead: What do you want to do? If an object has a reference to another object, then you can't replace the refernced object without changing that reference.

(You could look into the Proxy design pattern but I don't know if this will be helpful for you right now.)
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so here's what led me down this train of thought...

I wrote a linked list class with String payloads, then wrote a new class that extends the first class to make them hold Strings formatted in a particular way. The constructor accepts a variable argument, formats it appropriately, and then stores it in the list. The problem comes when I want to update the contents of the list. The only method that handles variable arguments is the constructor so I can't just use insertAtHead() or even getHead().setData() unless I rewrite what it is the constructor's doing.

I guess I could do that, but I'm trying to practice with inheritance and code reuse.

This led me to the thought that if I simply made a new instance of the object and reassigned the reference to the new one, all would be well. This obviously didn't work, so now I'm trying to collect my thoughts and figure out which way to go.

Did that make sense?
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My thought was basically to instantiate a new object with the desired data and change the reference from the original instance to the new one. It seems like such a simple one liner, but does not work in practice. I feel like I'm making this more complicated than it needs to be.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Morris wrote:Did that make sense?


No. Particularly not this bit:

The only method that handles variable arguments is the constructor


Perhaps posting the code would help?

 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, here's what I did.



This solves my problem, but does not answer my original question. If 'this' provides a reference to the current object (at the object level) I don't understand why it can't be reassigned. Is it final? Is it not really providing a direct reference to the object? I don't understand the underlying problem.
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:As you can't change the context you can't change the this reference.


This post comes the closest, I think, to explaining what the underlying problem is. What do you mean by the context? It seems like the reference 'this' is not really giving access to the primary reference to the object. In other words, 'this' does not give you 'myObject', but rather points you toward the 'myObject' reference. Is that right?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me point out that in some language other than Java (I don't remember which, maybe it was Visual Basic), the keyword for the current object is "Me". Now obviously you can't change the meaning of "Me" to be some other object.

Besides, in English the meaning of "this" is fixed too. When I talk about "this forum" here I'm talking about the Ranch, and when I talk about "this forum" on the South African National Parks forum, I mean the SANP forum. There isn't any way for me to say on this forum "Now when I say <this forum> I don't mean the Ranch any more, I mean the SANP forum instead."
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting away from the this-thing for a bit, let's reexamine your approach.

You have code that's not reusable because it's in a constructor. So rather than try some convoluted "replacement" approach to reuse this unreusable code, why not just move the code out of the constructor and into a reusable utility method?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Morris wrote:
Hauke Ingmar Schmidt wrote:As you can't change the context you can't change the this reference.


This post comes the closest, I think, to explaining what the underlying problem is. What do you mean by the context? It seems like the reference 'this' is not really giving access to the primary reference to the object. In other words, 'this' does not give you 'myObject', but rather points you toward the 'myObject' reference. Is that right?


No.

There is no such thing as a "primary reference". An object lives on the heap (memory), independent of how many references pointing to it (when the number is zero, the object may be removed as it is unreachable).

References are variables that belong to some context (or scope). A reference variable can be a field of an class or a variable in a method body. This defines which other variables or fields are visible at that moment. When you are inside a (non-static) method you are working with a specific object (that you called the method on). If you need a reference to the object itself -the object the method was called on- you can use the this reference. But as long as you are inside this method the value of this can't change - you are not leaving the method and the object that the method was called on.

So if you define two references to the same object there is no hierarchy and no connection between them. They just happen to point to the same object on the heap. In consequence there is no idiom for "let any reference that points to object a now point to object b". If you need such you need a layer of indirection, i.e. you don't reference the object itself, but an object that knows where the object to use at calling time is located. This can be done by applying a design pattern called "Proxy".
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your biggest misunderstanding here is believing that this is a variable. It isn't. It is a keyword representing the current instance of the class. It is at the same level as for or if. Would you expect the line for = new Object(); to work?
 
Matt Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:
There is no such thing as a "primary reference". An object lives on the heap (memory), independent of how many references pointing to it (when the number is zero, the object may be removed as it is unreachable).

References are variables that belong to some context (or scope). A reference variable can be a field of an class or a variable in a method body. This defines which other variables or fields are visible at that moment. When you are inside a (non-static) method you are working with a specific object (that you called the method on). If you need a reference to the object itself -the object the method was called on- you can use the this reference. But as long as you are inside this method the value of this can't change - you are not leaving the method and the object that the method was called on.

So if you define two references to the same object there is no hierarchy and no connection between them. They just happen to point to the same object on the heap. In consequence there is no idiom for "let any reference that points to object a now point to object b". If you need such you need a layer of indirection, i.e. you don't reference the object itself, but an object that knows where the object to use at calling time is located. This can be done by applying a design pattern called "Proxy".


Hauke, you are absolutely right. It makes perfect sense now that you put it like that. The this keyword returns a reference to the current object, not to the reference (i.e. myObject).

@Steve Of course I wouldn't expect for = new Object(); to work. It was my understanding that the 'this' keyword provides a reference to the current object. The flaw in my thought process lay in what it was I thought 'this' was actually providing reference to. It points to the object, and is not in any way connected with any of the other references to that object.

Hauke, thank you for sticking with me until I sorted this out.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between references and objects is one which every new Java programmer has to learn the hard way. But once you understand the difference, all of a sudden things which were confusing become obvious.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!