Java Observable addObserver() Method



Description

The Java Observable addObserver(Observer o) method adds the specified observer o to the set of observers for this object if, the observer is not the same as some observer already in the set.

Declaration

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

public void addObserver(Observer o)

Parameters

o − The observer to be added.

Return Value

NA

Exception

NullPointerException − if the specified observer is null.

Adding Observer for a String Value Change Example

The following example shows the usage of java.util.Observable.addObserver(Observer) method. We've created an ObservedObject class by extending Observable class and then overridden its method setValue(). 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();
         // 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

Adding Observer for an Integer Value Change Example

The following example shows the usage of java.util.Observable.addObserver(Observer) method. We've created an ObservedObject class by extending Observable class and then overridden its method setValue(). 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();
         // trigger notification
         notifyObservers(value);
      }
   }
}

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

      // trigger value change
      watched.setValue(2);

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

      // trigger value change
      watched.setValue(3);
   }
      
   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: 2
Value changed to new value: 3
Update called with Arguments: 3

Adding Observer for an Object Value Change Example

The following example shows the usage of java.util.Observable.addObserver(Observer) method. We've created an ObservedObject class by extending Observable class and then overridden its method setValue(). 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();
         // trigger notification
         notifyObservers(value);
      }
   }
}

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

      // trigger value change
      watched.setValue(new Student(2, "Robert"));

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

      // trigger value change
      watched.setValue(new Student(3, "Adam"));
   }
      
   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 −

Value changed to new value: [ 2, Robert ]
Value changed to new value: [ 3, Adam ]
Update called with Arguments: [ 3, Adam ]
java_util_observable.htm
Advertisements