Java Observable.deleteObservers()

Syntax

Observable.deleteObservers() has the following syntax.

public void deleteObservers()

Example

In the following code shows how to use Observable.deleteObservers() method.


//from   w w w  . j  av a2s  .c  o  m
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(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;
         
         // mark as value changed
         setChanged();
         notifyObservers(value);
      }
   }
}
class MainObserver implements Observer {

   
   public void update(Observable obj, Object arg) {
      System.out.println(obj+" called with Arguments: "+arg);
   }
}
public class Main{
   public static void main(String[] args) {
      ObservedObject watched = new ObservedObject("Original Value");
      MainObserver watcher1 = new MainObserver();
      MainObserver watcher2 = new MainObserver();

      watched.setValue("Value before addObserver");

      watched.addObserver(watcher1);
      watched.addObserver(watcher2);

      watched.setValue("Value after addObserver");
      watched.deleteObservers();
      watched.setValue("Value after deleteObservers");
   }
}

The code above generates the following result.