Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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?
 
Daniel Renner
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator

Thank you Daniel. Something to read about then: References. Have not done much Java lately. time to catch up. =)
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

Ah thanks for correcting me. Thumbs up!
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic