Java Observable .notifyObservers (Object arg)
Syntax
Observable.notifyObservers(Object arg) has the following syntax.
public void notifyObservers(Object arg)
Example
In the following code shows how to use Observable.notifyObservers(Object arg) method.
//w ww.j a v a 2 s .c o m
import java.util.Observable;
import java.util.Observer;
class ObservedObject extends Observable {
private String watchedValue;
public ObservedObject(String value) {
watchedValue = value;
}
public void setValue(String value) {
if(!watchedValue.equals(value)) {
System.out.println("Value changed to new value: "+value);
watchedValue = value;
setChanged();
notifyObservers(value);
}
}
}
class MainObserver implements Observer {
public void update(Observable obj, Object arg) {
System.out.println(obj+" called with Arguments: "+arg);
}
}
public class Main{
public static void main(String[] args) {
ObservedObject watched = new ObservedObject("Original Value");
MainObserver watcher = new MainObserver();
watched.addObserver(watcher);
watched.setValue("New Value");
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »