CHAPTER 21: The Package java.util Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

21.7 The Class java.util.Observable

21.7.1 addObserver , 21.7.2 deleteObserver , 21.7.3 deleteObservers , 21.7.4 countObservers , 21.7.5 notifyObservers , 21.7.6 notifyObservers , 21.7.7 setChanged , 21.7.8 clearChanged , 21.7.9 hasChanged

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 (S21.8).

Note that this notification mechanism is has nothing to do with threads (S20.20) and is completely separate from the wait and notify mechanism of class Object (S20.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 (S20.1.3) returns true for them.


21.7.1 addObserver

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 deleteObserver

public void deleteObserver(Observer o)

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


21.7.3 deleteObservers

public void deleteObservers()

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


21.7.4 countObservers

public int countObservers()

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


21.7.5 notifyObservers

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 notifyObservers

public void notifyObservers(Object arg)

If this Observable object has been marked as changed (S21.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 (S21.8.1) on two arguments: this Observable object and arg. The mark on this object is then cleared (S21.7.8).


21.7.7 setChanged

protected void setChanged()

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


21.7.8 clearChanged

protected void clearChanged()

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


21.7.9 hasChanged

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.

Top© 1996 Sun Microsystems, Inc. All rights reserved.