Java Observable clearChanged() Method



Description

The Java Observable clearChanged(Observer o) method indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent change. This method is called automatically by the notifyObservers methods.

Declaration

Following is the declaration for Java.util.Observable.clearChanged() method

protected void clearChanged()

Parameters

NA

Return Value

NA

Exception

NA

Clearing Change for Observer for a String Value Change Example

The following example shows the usage of java.util.Observable.clearChanged() method. We've created an ObservedObject class by extending Observable class and then overridden its method resetValue() calling the clearChanged() method. In main class, We've added the observer using addObserver() method and in output, we've printed the updates happening to the value of ObservedObject.

package com.tutorialspoint;

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 value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
      }
   }
	  
   public void resetValue() {
      // reset value changed flag
       clearChanged();
   }   
}

public class ObservableDemo implements Observer {
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject("Original Value");
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");
      
      // add observer to the watched object
      watched.addObserver(watcher);

      // trigger value change
      System.out.println("setValue method called...");
      watched.setValue("New Value");
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed");
      }
      
      // trigger reset
      System.out.println("resetValue method called...");
      watched.resetValue();
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed"); 
      }
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println("Update called with Arguments: "+arg);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

setValue method called...
Value changed
resetValue method called...
Value not changed

Clearing Change for Observer for an Integer Value Change Example

The following example shows the usage of java.util.Observable.clearChanged() method. We've created an ObservedObject class by extending Observable class and then overridden its method resetValue() calling the clearChanged() method. In main class, We've added the observer using addObserver() method and in output, we've printed the updates happening to the value of ObservedObject.

package com.tutorialspoint;

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private Integer watchedValue;
   
   public ObservedObject(Integer value) {
      watchedValue = value;
   }
      
   public void setValue(Integer value) {

      // if value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
      }
   }  
   public void resetValue() {
      // reset value changed flag
      clearChanged();
   }
}

public class ObservableDemo implements Observer {
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject(1);
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");
      
      // add observer to the watched object
      watched.addObserver(watcher);

      // trigger value change
      System.out.println("setValue method called...");
      watched.setValue(2);
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed");
      }
      
      // trigger reset
      System.out.println("resetValue method called...");
      watched.resetValue();
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed"); 
      }
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println("Update called with Arguments: "+arg);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

setValue method called...
Value changed
resetValue method called...
Value not changed

Clearing Change for Observer for an Object Value Change Example

The following example shows the usage of java.util.Observable.clearChanged() method. We've created an ObservedObject class by extending Observable class and then overridden its method resetValue() calling the clearChanged() method. In main class, We've added the observer using addObserver() method and in output, we've printed the updates happening to the value of ObservedObject.

package com.tutorialspoint;

import java.util.Observable;
import java.util.Observer;

class ObservedObject extends Observable {
   private Student watchedValue;
   
   public ObservedObject(Student value) {
      watchedValue = value;
   }
      
   public void setValue(Student value) {
      // if value has changed notify observers
      if(!watchedValue.equals(value)) {
         System.out.println("Value changed to new value: "+value);
         watchedValue = value;

         // mark as value changed
         setChanged();
      }
   }  
	
   public void resetValue() {
      // reset value changed flag
      clearChanged();
   }
}

public class ObservableDemo implements Observer {
   public static void main(String[] args) {
      
      // create watched and watcher objects
      ObservedObject watched = new ObservedObject(new Student(1, "Julie"));
      
      // watcher object listens to object change
      ObservableDemo watcher = new ObservableDemo("Watcher");
      
      // add observer to the watched object
      watched.addObserver(watcher);

      // trigger value change
      System.out.println("setValue method called...");
      watched.setValue(new Student(2, "Robert"));
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed");
      }
      
      // trigger reset
      System.out.println("resetValue method called...");
      watched.resetValue();
      
      // check if value has changed
      if(watched.hasChanged()) {
         System.out.println("Value changed");
      } else {
         System.out.println("Value not changed"); 
      }
   }
      
   public void update(Observable obj, Object arg) {
      System.out.println("Update called with Arguments: "+arg);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

setValue method called...
Value changed
resetValue method called...
Value not changed
java_util_observable.htm
Advertisements