aspose file tools*
The moose likes Java in General and the fly likes Observable and Observer - Send class object with notifyObservers() and use the object atthe Observer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Observable and Observer - Send class object with notifyObservers() and use the object atthe Observer" Watch "Observable and Observer - Send class object with notifyObservers() and use the object atthe Observer" New topic
Author

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

chihwah li
Greenhorn

Joined: Apr 23, 2010
Posts: 25
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

Joined: Mar 29, 2012
Posts: 10
One solution:


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

Joined: Apr 23, 2010
Posts: 25
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

Joined: Mar 29, 2012
Posts: 10
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

Joined: Apr 23, 2010
Posts: 25

Thank you Daniel. Something to read about then: References. Have not done much Java lately. time to catch up. =)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chihwah li
Greenhorn

Joined: Apr 23, 2010
Posts: 25

Ah thanks for correcting me. Thumbs up!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're welcome.
 
jQuery in Action, 2nd edition
 
subject: Observable and Observer - Send class object with notifyObservers() and use the object atthe Observer