java.util.Observable is used to create subclasses that other parts of the program can observe. When an object of such subclass undergoes a change, observing classes are notified. The update( ) method is called when an observer is notified of a change.
Note: Observing class must implement the Observer interface, which defines the update( ) method.
An object that is being observed must follow two simple rules :
- If it is changed, it must call setChanged( ) method.
- When it is ready to notify observers of this change, it must call notifyObservers( ) method. This causes the update( ) method in the observing object(s) to be called.
Be careful, if the object calls notifyObservers( ) method without having previously called setChanged( ) method, no action will take place.
The observed object must call both setChanged( ) and notifyObservers( ) method, before update( ) will be called.
Constructor of java.util.Observable :
- Observable( )
Construct an Observable with zero Observers.
Methods:
- addObserver(Observer observer) : Adds observer to the list of objects observing the invoking object.
Syntax : public void addObserver(Observer observer) Exception : NullPointerException -> if the parameter observer is null
// Java code to demonstrate addObserver() methodimportjava.util.*;// This is the observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1 is added");}}// This is class being observedclassBeingObservedextendsObservable{voidincre(){setChanged();notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();beingObserved.addObserver(observer1);beingObserved.incre();}}chevron_rightfilter_noneOutput :
Observer1 is added
- setChanged() : Called when the invoking object has changed.
Syntax : protected void setChanged( ) Exception : NA.
// Java code to demonstrate setChanged() methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg) { }}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();System.out.println("Change status with setChanged :"+ hasChanged());notifyObservers();}voidfunc2(){System.out.println("Change status without setChanged :"+ hasChanged());notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){booleanstatus;BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();beingObserved.addObserver(observer1);beingObserved.func1();beingObserved.func2();}}chevron_rightfilter_noneOutput :
Change status with setChanged :true Change status without setChanged :false
- clearChanged(): Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change, so that the hasChanged( ) method will now return false.
Syntax : protected void clearChanged( ) Exception : NA
// Java code to demonstrate clearChanged() methodimportjava.util.*;// This is the observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Inside Observer1");}}// This is the class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();// clearChanged method removes all the changes made by setChanged methodclearChanged();notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();beingObserved.addObserver(observer1);beingObserved.func1();}}chevron_rightfilter_noneOutput :
No Output
No output is obtained because clearChanged( ) method has removed all the changes.
- notifyObservers() : Notifies all observers of the invoking object that it has changed by calling update( ).
A null is passed as the second argument to update( ).Syntax : public void notifyObservers( ) Exception : NA
// Java code to demonstrate notifyObservers( ) methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1 Notified");}}// This is second observerclassObserver2implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer2 Notified");}}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();/*This method notifies the change to all theobservers that are registered*/notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();Observer2 observer2 =newObserver2();beingObserved.addObserver(observer1);beingObserved.addObserver(observer2);beingObserved.func1();}}chevron_rightfilter_noneOutput :
Observer2 Notified Observer1 Notified
- notifyObservers(Object obj) : Notifies all observers of the invoking object that it has changed by calling update( ).
obj is passed as an argument to update( ).Syntax : public void notifyObservers(Object obj) Exception : NA
// Java code to demonstrate notifyObservers(Object obj) methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1 Notified with value : "+((Integer)arg).intValue());}}// This is second observerclassObserver2implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer2 Notified with value : "+((Integer)arg).intValue());}}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();/*This method notifies the change to all theobservers that are registered and passes an object*/notifyObservers(newInteger(10));}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();Observer2 observer2 =newObserver2();beingObserved.addObserver(observer1);beingObserved.addObserver(observer2);beingObserved.func1();}}chevron_rightfilter_noneOutput :
Observer2 Notified with value : 10 Observer1 Notified with value : 10
- countObservers( ) : Returns the number of objects observing the invoking object.
Syntax : public int countObservers( ) Returns : the number of observers of this object Exception : NA
// Java code to demonstrate countObservers() methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1");}}// This is second observerclassObserver2implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer2");}}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();Observer2 observer2 =newObserver2();beingObserved.addObserver(observer1);beingObserved.addObserver(observer2);intcount_observer = beingObserved.countObservers();System.out.println("Number of observers is "+ count_observer);beingObserved.func1();}}chevron_rightfilter_noneOutput :
Number of observers is 2 Observer2 Observer1
- deleteObserver(Observer observer): Removes observer from the list of objects observing the invoking object.
Passing null to this method will have no effect.Syntax : public void deleteObserver(Observer observer) Exception : NA
// Java code to demonstrate deleteObserver(Observer observer) methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1");}}// This is second observerclassObserver2implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer2");}}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();notifyObservers();}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){intcount_observer;BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();Observer2 observer2 =newObserver2();beingObserved.addObserver(observer1);beingObserved.addObserver(observer2);count_observer = beingObserved.countObservers();System.out.println("Number of observers before"+" calling deleteObserver(): "+ count_observer);beingObserved.func1();// Deleting observer1beingObserved.deleteObserver(observer1);count_observer = beingObserved.countObservers();System.out.println("No. of observers after"+" calling deleteObserver(): "+ count_observer);beingObserved.func1();}}chevron_rightfilter_noneOutput :
Number of observers before calling deleteObserver(): 2 Observer2 Observer1 No. of observers aftercalling deleteObserver(): 1 Observer2
- deleteObservers() : Removes all observers for the invoking object.
Syntax : public void deleteObservers() Exception : NA
// Java code to demonstrate deleteObservers() methodimportjava.util.*;// This is first observerclassObserver1implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer1");}}// This is second observerclassObserver2implementsObserver{publicvoidupdate(Observable obj, Object arg){System.out.println("Observer2");}}// This is class being observedclassBeingObservedextendsObservable{voidfunc1(){setChanged();notifyObservers(newInteger(10));}}classObserverDemo {// Driver method of the programpublicstaticvoidmain(String args[]){intcount_observer;BeingObserved beingObserved =newBeingObserved();Observer1 observer1 =newObserver1();Observer2 observer2 =newObserver2();beingObserved.addObserver(observer1);beingObserved.addObserver(observer2);count_observer = beingObserved.countObservers();System.out.println("Number of observers before"+" calling deleteObserver(): "+ count_observer);beingObserved.func1();// Deleting all observersbeingObserved.deleteObservers();count_observer = beingObserved.countObservers();System.out.println("No. of observers after "+"calling deleteObserver(): "+ count_observer);beingObserved.func1();}}chevron_rightfilter_noneOutput :
Number of observers before calling deleteObserver(): 2 Observer2 Observer1 No. of observers after calling deleteObserver(): 0
Ref: Observables in Java
This article is contributed by Dipak Chouhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java

