This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Observable not working properly... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Observable not working properly..." Watch "Observable not working properly..." New topic
Author

Observable not working properly...

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Consider the following piece of code...
import java.util.*;
public class TestObs{
public static void main(String[] a){
Worker worker = new Worker();
supervisor shekar = new supervisor();
worker.addObserver(shekar);
worker.i=50;
System.out.println(" Has change :"+worker.hasChanged());
}
}

class supervisor implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed");
}

}
class Worker extends Observable{
public int i=10;
public void notifyObservers(){
System.out.println("change has occured in the observed object");
}
}

in the above code i have changed the data in the observable object worker, i.e the value of i to 50. This is a change in the data of the observable object. When i do that the notifyObservers should run and the hasChanged method should return a true value but the value that is returned is false.
How can I correct my method or is there something missing with my understanding. Kindly help me out if I have understood the observable correctly.
Thanks and regards.
shekar.


Thanks,
Shekar
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
The javadocs are not a model of clarity on this, but you need to setChanged() in your Observable and then you need to actually notifyObservers(). It's in the notifyObservers that you need to call hasChanged() and clearChanged() but the simplest way to achieve this is to call super.notifyObservers() when you over-ride. So for example have a setValue method in Worker to handle your change to variable i:

This will call your update method in Supervisor.
Regards,
Kenny
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Thanks for the reply
It is working fine. but i still have some problems with the following code.
import java.util.*;
public class TestObs{
public static void main(String[] a){
Worker worker = new Worker();
supervisor shekar = new supervisor();
worker.addObserver(shekar);
supervisor shekar1 = new supervisor();
worker.addObserver(shekar1);
supervisor shekar2 = new supervisor();
worker.addObserver(shekar2);

System.out.println(" No of observers :"+worker.countObservers());

worker.setValue(50);
System.out.println(" Has change :"+worker.hasChanged());
}
}

class supervisor implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed");
}

}
class supervisor1 implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed1");
}

}
class supervisor2 implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed2");
}

}
class Worker extends Observable{
public int i=10;
public void setValue(int val)
{
i=val;
setChanged();
// System.out.println(" Has change :"+worker.hasChanged());
notifyObservers();
}
public void notifyObservers()
{
super.notifyObservers();
System.out.println(" Look boss, I'm doing some work!");
}
}

In the above code when I have added more than one observer for all the observers the update method in supervisor is getting called. I guess this is wrong because the corresponding observer's method should be called. Also when I say hasChanged it is giving the value false.
What could be problem and will update of every observer execute or only the update of particular observer exectutes for the changes .

thanks and regards,
shekar.
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
You've probably already worked it out;
anyway, according to your code you have created three instances of supervisor rather than one of each of your three different types of observers.
Regards,
Kenny
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Sorry, didn't answer your question about hasChanged.
Remember that super.notifyObservers() checks hasChanged() and then clears it. This means that in setValue after setChanged() a call to this.hasChanged() will return true. (your code has worker.hasChanged() which won't compile if you un-comment it)
However, in your main method a call to worker.hasChanged() will return false after setValue because the call to notifyObservers() in setValue has already cleared the change.
Regards,
Kenny
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Observable not working properly...
 
Similar Threads
Observer's code example
Umm, how does Observer work?
I want observer/observable implemented in Servlet/Applet
Shared object / concurrency
ViewPart and Observer