Bookmark Topic Watch Topic
  • New Topic

Need help in understanding "this" code  RSS feed

 
shivang sarawagi
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Report post to moderator


Can anyone help me in understanding the code inside the constructor



registerObserver is a method in another class

Thanks
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Report post to moderator
'this' is a keyword in java. It refers to the instance of the object in which the code is currently running.

To create a CurrentConditionsDisplay object in another class, you would use (evidently)


and after that, ccd would be a reference to the object instance you had created. The 'this' keyword refers to that object inside the CurrentConditionsDisplay code.

Evidently the registerObserver() method can take a reference to a CurrentConditionsDisplay object, probably it requires Observer. One has to be careful using 'this' within constructors, since, until the constructor has exited, the object is not fully formed. However, near the end of the constructor and just for purposes of putting it on an observer list, it's probably fine as illustrated here.

rc
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
There is a similar discussion here.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Campbell Ritchie wrote:There is a similar discussion here.

Ah, so we now have circular references. Since both Threads are by the same OP, might it not be better to remove one?

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
Winston Gutkowski wrote:Ah, so we now have circular references. Since both Threads are by the same OP, might it not be better to remove one?

Winston


yup.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!