java.util.Observer is an interface that represents the observer in the Observer design pattern.
- Objects that implement this interface can subscribe to an
Observableobject and be notified of any state changes. - It works with
java.util.Observableto implement the publish-subscribe model.
Deprecated since Java 9 — replaced by modern event systems and reactive frameworks like Java Flow API, RxJava, and JavaBeans PropertyChangeListener.
Interface Signature
public interface Observer {
void update(Observable o, Object arg);
}Code language: PHP (php)
Methods

The above method is invoked by Observable.notifyObservers().
Simple Program
import java.util.Observable;
import java.util.Observer;
class WeatherData extends Observable {
public void temperatureChanged(float temp) {
setChanged(); // Mark as changed
notifyObservers(temp); // Notify all observers
}
}
class TemperatureDisplay implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("Temperature updated: " + arg + "°C");
}
}
public class WeatherApp {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
TemperatureDisplay display = new TemperatureDisplay();
weatherData.addObserver(display);
weatherData.temperatureChanged(28.5f);
weatherData.temperatureChanged(32.0f);
}
}Output
Temperature updated: 28.5°C
Temperature updated: 32.0°CCode language: CSS (css)
Problem Statement:
LotusJavaPrince is developing a Banking Notification System.Mahesh subscribes to different bank alerts: low balance, large withdrawal, and loan EMI due.When the system updates, all observers (Mahesh’s alert handlers) must receive notifications instantly.
import java.util.Observable;
import java.util.Observer;
// Observable class (Bank System)
class BankSystem extends Observable {
public void triggerAlert(String message) {
setChanged();
notifyObservers(message);
}
}
// Observer 1: Balance Alert
class BalanceAlert implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Balance Alert for Mahesh → " + arg);
}
}
// Observer 2: Withdrawal Alert
class WithdrawalAlert implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Withdrawal Alert for Mahesh → " + arg);
}
}
// Observer 3: Loan EMI Alert
class LoanEMIAlert implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Loan EMI Alert for Mahesh → " + arg);
}
}
public class BankAlertApp {
public static void main(String[] args) {
BankSystem bank = new BankSystem();
BalanceAlert balanceObserver = new BalanceAlert();
WithdrawalAlert withdrawalObserver = new WithdrawalAlert();
LoanEMIAlert loanObserver = new LoanEMIAlert();
// Register observers
bank.addObserver(balanceObserver);
bank.addObserver(withdrawalObserver);
bank.addObserver(loanObserver);
// Trigger alerts
bank.triggerAlert("Balance below ₹1000 for Mahesh.");
bank.triggerAlert("Mahesh withdrew ₹50,000.");
bank.triggerAlert("Loan EMI of ₹7,500 due for Mahesh.");
}
}Output
Balance Alert for Mahesh → Balance below ₹1000 for Mahesh.
Withdrawal Alert for Mahesh → Balance below ₹1000 for Mahesh.
Loan EMI Alert for Mahesh → Balance below ₹1000 for Mahesh.
Balance Alert for Mahesh → Mahesh withdrew ₹50,000.
Withdrawal Alert for Mahesh → Mahesh withdrew ₹50,000.
Loan EMI Alert for Mahesh → Mahesh withdrew ₹50,000.
Balance Alert for Mahesh → Loan EMI of ₹7,500 due for Mahesh.
Withdrawal Alert for Mahesh → Loan EMI of ₹7,500 due for Mahesh.
Loan EMI Alert for Mahesh → Loan EMI of ₹7,500 due for Mahesh.
Even though Observer is outdated, learning it builds a strong OO foundation and helps understand event-based architecture, which is critical for designing systems
