wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Umm, how does Observer work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Umm, how does Observer work?" Watch "Umm, how does Observer work?" New topic
Author

Umm, how does Observer work?

John Blanco
Greenhorn

Joined: Jan 13, 2005
Posts: 20
I feel like such a dunce. What's wrong with this code? The update() never gets called?

import java.util.*;

public class Whatever implements Observer {
public static void main(String[] args) {
new Whatever();
}

public Whatever() {
Observable obj = new Observable();

obj.addObserver(this);
obj.notifyObservers();
}

public void update (Observable o, Object obj) {
System.out.println("Why isn't this called?!?!?!");
}

}
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Where are you calling it???


ASCII silly question, Get a silly ANSI.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

update() will be called by the Observable if someone ever calls the notifyObservers() method. Since the Observable is local to your Observer's constructor, there's no way this will ever happen.

Normally the way these things are used is that you make a subclass of Observable, Observers register with it, and then the subclass calls notifyObservers() on itself when "something happens" -- i.e., when there is an event that it wants the Observers to know about.


[Jess in Action][AskingGoodQuestions]
John Blanco
Greenhorn

Joined: Jan 13, 2005
Posts: 20
The example is contrived...in actual practice, it's useful. I don't see why this code, however, doesn't work? Yes, the Observable is local, but it still registers and then notifies an observer.

It should work.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes, the example is convoluted, but notifyObservers() is indeed called. The real problem is that setChanged() has never been called. The API is not very clear about this, but when notifyObservers() is called, the first thing it does is check whether the object has been "changed" (meaning, has setChanged() been called) and if not, it does nothing. Here's an example that works:


"I'm not back." - Bill Harding, Twister
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It should also be noted that java.util.Observer and Observable aren't God's
gift to OOP. (For example, on either's javadoc page, go to the top and click
on the USE link. They aren't used in the rest of the Java API!) There's not a
lot of code in Observable, so if you want to code the Observer pattern from
scratch or using, say, SwingPropertyChangeSupport, go for it.
[ November 22, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by John Blanco:
it still ... notifies an observer.


You're right -- sorry for the oversight.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Umm, how does Observer work?