• 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
  • 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

Observable and Observer - Send class object with notifyObservers() and use the object atthe Observer

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I have a question about Observable class. I heard it's possible to send an object with the method notifyObservers();
At this moment I am sending a string, that works fine, but I like to send an object that my Observer class can read this objects variable value.

So far I have the following pices of code:

My Observer (a clock) it's update method:



My Observable class TimeGenerator that updates my clocks every 1 sec:


As you can see I am sending the class TimeGenerator as argument. The Observer class checks if it's a class of TimeGenerator.
Now I like to read the information from this arg object.

I tried making a method in TimeGenerator:



But I cannot access it with the statement : arg.getSecond();
I like to keep the field private.

How can I read the field of the object TimeGenerato that is send?
 
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One solution:


You can cast the object without any problems (because you checked the arg with (here f.e.) instanceof.
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Renner wrote:One solution:


You can cast the object without any problems (because you checked the arg with (here f.e.) instanceof.




Thanks for the solution. ^^! Exellent

A new question =)

Why do I have to store it locally with TimeGenerator t = (TimeGenerator) arg; And then access the method?? Why can't I use arg.getSecond()?
What theory did I miss to understand this?
 
Daniel Renner
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you set the Type of the arg-variable to Object. If you change it to TimeGenerator then you dont have to cast it .
A variable can only access methods from the defined Class. Thats why you need to cast some variables in some cases. The class Object dont know the method getSecond(), thats why you cannot call it. So i know your next question will be: "But he knows that its an instance of TimeGenerator, why doesnt he search after the method then?". Remember that you are using references!

But you dont need to define a new variable, you can also try to access the method as follows:
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Daniel. Something to read about then: References. Have not done much Java lately. time to catch up. =)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chihwah li wrote:As you can see I am sending the class TimeGenerator as argument.


No you're not. You're sending an instance of class TimeGenerator, not the class itself. Daniel already showed you how to solve that (using instanceof).
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ah thanks for correcting me. Thumbs up!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic