Java.util.Observable class in Java - GeeksforGeeks

08 Jul, 2017
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.
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() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 is added");    }}  // This is class being observedclass BeingObserved extends Observable{    void incre()     {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.incre();    }}Output :Observer1 is addedsetChanged() : Called when the invoking object has changed.Syntax : protected void setChanged( )
Exception : NA.
// Java code to demonstrate setChanged() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg) { }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        System.out.println("Change status with setChanged :" + hasChanged());        notifyObservers();    }          void func2()    {        System.out.println("Change status without setChanged :" + hasChanged());        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        boolean status;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();        beingObserved.func2();    }}Output :Change status with setChanged :true
Change status without setChanged :falseclearChanged(): 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() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Inside Observer1");    }}  // This is the class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        // clearChanged method removes all the changes made by setChanged method        clearChanged();        notifyObservers();              }}  class ObserverDemo {// Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();    }}Output :No OutputNo 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( ) methodimport java.util.*;   // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered*/        notifyObservers();              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}Output :Observer2 Notified
Observer1 NotifiednotifyObservers(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) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered and passes an object*/        notifyObservers(new Integer(10));              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}Output :Observer2 Notified with value : 10
Observer1 Notified with value : 10countObservers( ) : 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() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        int count_observer = beingObserved.countObservers();        System.out.println("Number of observers is " + count_observer);        beingObserved.func1();    }}Output :Number of observers is 2
Observer2
Observer1deleteObserver(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) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);                  count_observer = beingObserved.countObservers();        System.out.println("Number of observers before" +         " calling deleteObserver(): " + count_observer);        beingObserved.func1();                  // Deleting observer1        beingObserved.deleteObserver(observer1);        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after"+         " calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}Output :Number of observers before calling deleteObserver(): 2
Observer2
Observer1
No. of observers aftercalling deleteObserver(): 1
Observer2deleteObservers() : Removes all observers for the invoking object.Syntax : public void deleteObservers()
Exception : NA
// Java code to demonstrate deleteObservers() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers(new Integer(10));    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        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 observers        beingObserved.deleteObservers();        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after "+         "calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}Output :Number of observers before calling deleteObserver(): 2
Observer2
Observer1
No. of observers after calling deleteObserver(): 0
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() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 is added");    }}  // This is class being observedclass BeingObserved extends Observable{    void incre()     {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.incre();    }}Output :Observer1 is added
Syntax : public void addObserver(Observer observer)
Exception : NullPointerException -> if the parameter observer is null

// Java code to demonstrate addObserver() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 is added");    }}  // This is class being observedclass BeingObserved extends Observable{    void incre()     {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.incre();    }}
Output :
Observer1 is added
setChanged() : Called when the invoking object has changed.Syntax : protected void setChanged( )
Exception : NA.
// Java code to demonstrate setChanged() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg) { }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        System.out.println("Change status with setChanged :" + hasChanged());        notifyObservers();    }          void func2()    {        System.out.println("Change status without setChanged :" + hasChanged());        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        boolean status;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();        beingObserved.func2();    }}Output :Change status with setChanged :true
Change status without setChanged :false
Syntax : protected void setChanged( )
Exception : NA.

// Java code to demonstrate setChanged() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg) { }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        System.out.println("Change status with setChanged :" + hasChanged());        notifyObservers();    }          void func2()    {        System.out.println("Change status without setChanged :" + hasChanged());        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        boolean status;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();        beingObserved.func2();    }}
Output :
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() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Inside Observer1");    }}  // This is the class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        // clearChanged method removes all the changes made by setChanged method        clearChanged();        notifyObservers();              }}  class ObserverDemo {// Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();    }}Output :No OutputNo output is obtained because clearChanged( ) method has removed all the changes.
Syntax : protected void clearChanged( )
Exception : NA

// Java code to demonstrate clearChanged() methodimport java.util.*;  // This is the observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Inside Observer1");    }}  // This is the class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        // clearChanged method removes all the changes made by setChanged method        clearChanged();        notifyObservers();              }}  class ObserverDemo {// Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        beingObserved.addObserver(observer1);        beingObserved.func1();    }}
Output :
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( ) methodimport java.util.*;   // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered*/        notifyObservers();              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}Output :Observer2 Notified
Observer1 Notified
Syntax : public void notifyObservers( )
Exception : NA

// Java code to demonstrate notifyObservers( ) methodimport java.util.*;   // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered*/        notifyObservers();              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}
Output :
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) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered and passes an object*/        notifyObservers(new Integer(10));              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}Output :Observer2 Notified with value : 10
Observer1 Notified with value : 10
Syntax : public void notifyObservers(Object obj)
Exception : NA

// Java code to demonstrate notifyObservers(Object obj) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2 Notified with value : " +                 ((Integer)arg).intValue());    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        /*This method notifies the change to all the         observers that are registered and passes an object*/        notifyObservers(new Integer(10));              }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        beingObserved.func1();    }}
Output :
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() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        int count_observer = beingObserved.countObservers();        System.out.println("Number of observers is " + count_observer);        beingObserved.func1();    }}Output :Number of observers is 2
Observer2
Observer1
Syntax : public int countObservers( )
Returns : the number of observers of this object
Exception : NA

// Java code to demonstrate countObservers() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);        int count_observer = beingObserved.countObservers();        System.out.println("Number of observers is " + count_observer);        beingObserved.func1();    }}
Output :
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) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);                  count_observer = beingObserved.countObservers();        System.out.println("Number of observers before" +         " calling deleteObserver(): " + count_observer);        beingObserved.func1();                  // Deleting observer1        beingObserved.deleteObserver(observer1);        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after"+         " calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}Output :Number of observers before calling deleteObserver(): 2
Observer2
Observer1
No. of observers aftercalling deleteObserver(): 1
Observer2
Syntax : public void deleteObserver(Observer observer)
Exception : NA

// Java code to demonstrate deleteObserver(Observer observer) methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers();    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        beingObserved.addObserver(observer1);        beingObserved.addObserver(observer2);                  count_observer = beingObserved.countObservers();        System.out.println("Number of observers before" +         " calling deleteObserver(): " + count_observer);        beingObserved.func1();                  // Deleting observer1        beingObserved.deleteObserver(observer1);        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after"+         " calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}
Output :
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() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers(new Integer(10));    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        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 observers        beingObserved.deleteObservers();        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after "+         "calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}Output :Number of observers before calling deleteObserver(): 2
Observer2
Observer1
No. of observers after calling deleteObserver(): 0
Syntax : public void deleteObservers()
Exception : NA

// Java code to demonstrate deleteObservers() methodimport java.util.*;  // This is first observerclass Observer1 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer1");    }}  // This is second observerclass Observer2 implements Observer{    public void update(Observable obj, Object arg)     {        System.out.println("Observer2");    }}  // This is class being observedclass BeingObserved extends Observable{    void func1()    {        setChanged();        notifyObservers(new Integer(10));    }}  class ObserverDemo {    // Driver method of the program    public static void main(String args[])     {        int count_observer;        BeingObserved beingObserved = new BeingObserved();        Observer1 observer1 = new Observer1();        Observer2 observer2 = new Observer2();        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 observers        beingObserved.deleteObservers();        count_observer = beingObserved.countObservers();        System.out.println("No. of observers after "+         "calling deleteObserver(): " + count_observer);        beingObserved.func1();              }}
Output :
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.
Java - util package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
LinkedList in Java
Collections in Java