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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in understanding "this" code

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


Can anyone help me in understanding the code inside the constructor



registerObserver is a method in another class

Thanks
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There is a similar discussion here.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic