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.