Contents | Prev | Next | Index

21.7 The Class java.util.Observable

Each instance of class Observable maintains a set of "observers" that are notified whenever the Observable object changes in some significant way. An observer may be any object that implements interface Observer (§21.8).

Note that this notification mechanism is has nothing to do with threads (§20.20) and is completely separate from the wait and notify mechanism of class Object (§20.1).

public class Observable {
	public void addObserver(Observer o);
	public void deleteObserver(Observer o);
	public void deleteObservers();
	public int countObservers();
	public void notifyObservers();
	public void notifyObservers(Object arg);
	protected void setChanged();
	protected void clearChanged();
	public boolean hasChanged();
}
When an observable object is newly created, its set of observers is empty.

Two observers are considered the same if and only if the equals method (§20.1.3) returns true for them.

21.7.1 public void addObserver(Observer o)

The observer o is added to this Observable object's set of observers, provided that it is not the same as some observer already in the set.

21.7.2 public void deleteObserver(Observer o)

The observer o is removed from this Observable object's set of observers.

21.7.3 public void deleteObservers()

All observers are removed from this Observable object's set of observers.

21.7.4 public int countObservers()

The number of observers in this Observable object's set of observers is returned.

21.7.5 public void notifyObservers()

If this Observable object has been marked as changed, this method causes all observers to be notified with null as the second argument; in other words, this method is equivalent to:

notifyObservers(null)

21.7.6 public void notifyObservers(Object arg)

If this Observable object has been marked as changed (§21.7.9), this method causes all observers to be notified with arg as the second argument. An observer is notified by calling its update method (§21.8.1) on two arguments: this Observable object and arg. The mark on this object is then cleared (§21.7.8).

21.7.7 protected void setChanged()

This Observable object is marked as having been changed; the hasChanged method will now return true.

21.7.8 protected void clearChanged()

This Observable object is marked as not having been changed; the hasChanged method will now return false.

21.7.9 public boolean hasChanged()

The result is true if and only if the setChanged method has been called for this Observable object more recently than either the clearChanged method or the notifyObservers method.

21.8 The Interface java.util.Observer

A class should implement the Observer interface if it is to be notified whenever an Observable object has been changed. See the Observable class (§21.7) for a discussion of how Observer objects are notified.

public interface Observer {
	public void update(Observable o, Object arg);
}

21.8.1 public void update(Observable o, Object arg)

When an Observable object has been changed and its notifyObservers method (§21.7.6) is called, every Observer object in its set of observers is notified by invoking its update method, passing it two arguments: the Observable object and another argument specified by the call to the notifyObservers method.


Contents | Prev | Next | Index

Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com