GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java Observable Class
Selected Reading
Java Observable ClassIntroductionThe Java Observable class represents an observable object, or "data" in the model-view paradigm.Following are the important points about Observable −
Class declarationFollowing is the declaration for java.util.Observable class − public class Observable extends Object Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Adding Observer and getting updates using Observer ExampleThe following example shows the usage of java.util.Observable.addObserver(Observer) method. package com.tutorialspoint;
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 value has changed notify observers
if(!watchedValue.equals(value)) {
System.out.println("Value changed to new value: "+value);
watchedValue = value;
// mark as value changed
setChanged();
// trigger notification
notifyObservers(value);
}
}
}
public class ObservableDemo implements Observer {
public static void main(String[] args) {
// create watched and watcher objects
ObservedObject watched = new ObservedObject("Original Value");
// watcher object listens to object change
ObservableDemo watcher = new ObservableDemo();
// trigger value change
watched.setValue("New Value");
// add observer to the watched object
watched.addObserver(watcher);
// trigger value change
watched.setValue("Latest Value");
}
public void update(Observable obj, Object arg) {
System.out.println("Update called with Arguments: "+arg);
}
}
OutputLet us compile and run the above program, this will produce the following result − Value changed to new value: New Value Value changed to new value: Latest Value Update called with Arguments: Latest Value Advertisements
| ||||||||||||||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |