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.