Java Observable Class



Introduction

The Java Observable class represents an observable object, or "data" in the model-view paradigm.Following are the important points about Observable −

  • The class can be subclassed to represent an object that the application wants to have observed.

  • An observable object can have one or more observers.

Class declaration

Following is the declaration for java.util.Observable class −

public class Observable
   extends Object

Class constructors

Sr.No. Constructor & Description
1

Observable()

This constructs an Observable with zero Observers.

Class methods

Sr.No. Method & Description
1 void addObserver(Observer o)

This method adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set.

2 protected void clearChanged()

This method 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.

3 int countObservers()

This method returns the number of observers of this Observable object.

4 void deleteObserver(Observer o)

This method deletes an observer from the set of observers of this object.

5 void deleteObservers()

This method clears the observer list so that this object no longer has any observers.

6 boolean hasChanged()

This method tests if this object has changed.

7 void notifyObservers()

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

8 void notifyObservers(Object arg)

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

9 protected void setChanged()

This method returns marks this Observable object as having been changed; the hasChanged method will now return true.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Adding Observer and getting updates using Observer Example

The following example shows the usage of java.util.Observable.addObserver(Observer) method.

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();
         // trigger notification
         notifyObservers(value);
      }
   }
}

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();

      // trigger value change
      watched.setValue("New Value");

      // add observer to the watched object
      watched.addObserver(watcher);

      // trigger value change
      watched.setValue("Latest Value");
   }
      
   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 −

Value changed to new value: New Value
Value changed to new value: Latest Value
Update called with Arguments: Latest Value
Advertisements