Recommended Free Tools
In Java 8, the clearest way to implement the Observer pattern is usually a small subject class with an explicit subscription API and a typed callback. Java 8 lambdas make observers concise; you can also use java.util.Observer and java.util.Observable, but Oracle deprecated both in Java 9 and documents limitations in their notification behavior.
What the Observer pattern does
A subject owns changing state or emits events. Observers subscribe to the subject, receive notifications when relevant changes occur, and can unsubscribe when they no longer need them. This separates the source of an update from the code that reacts to it.
Implement a typed observer in Java 8
A custom implementation lets you choose the payload type and define subscription behavior for your application. This example publishes a value to a snapshot of the current subscribers:
import java.util.ArrayList;
import java.util.List;
@FunctionalInterface
interface Observer<T> {
void onChange(T value);
}
final class Subject<T> {
private final List<Observer<T>> observers = new ArrayList<>();
void subscribe(Observer<T> observer) {
observers.add(observer);
}
void unsubscribe(Observer<T> observer) {
observers.remove(observer);
}
void publish(T value) {
for (Observer<T> observer : new ArrayList<>(observers)) {
observer.onChange(value);
}
}
}
Copying the list before iteration means a callback can add or remove a subscription without structurally changing the collection currently being traversed. It does not make this implementation thread-safe: concurrent calls to subscribe, unsubscribe, or publish still need an explicit synchronization or concurrency policy.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsDecide the callback contract
The example deliberately leaves several policies open. Decide and document them for the actual subject:
- Duplicates: Can the same observer subscribe more than once? The example allows duplicates; each registration receives a callback.
- Removal:
List.removeremoves one matching registration. If duplicates are permitted, unsubscribe behavior should be clear. - Order: An
ArrayListsnapshot is traversed in list order, but that is a choice of this implementation, not a universal Observer-pattern guarantee. - Callback failures: An exception from an observer escapes
publishand stops the loop, so later observers may not be called. Catch and handle exceptions per observer if that better fits the contract. - Threading: This code invokes callbacks synchronously on the thread calling
publish. Use a separate design if callbacks must be queued, coordinated across threads, or delivered with back-pressure.
Use lambdas or Consumer callbacks
Because Observer<T> has one abstract method, callers can provide its behavior with a lambda or method reference:
Rank #2
subject.subscribe(value -> logger.info("changed: {}", value));
subject.subscribe(System.out::println);
Java 8 defines Consumer<T> as a functional interface whose accept(T) method performs an action without returning a result. For a generic one-way callback, the subject can store Consumer<T> instead of a custom interface. Prefer a domain-specific callback such as onChange(State state) when its name and parameter clarify what the event means. Oracle describes lambdas as a way to encapsulate behavior and pass it to other code, and method references as compact calls to existing methods. Oracle: Java Programming Language Enhancements for Java SE 8; Oracle: Consumer<T>.
Should you use Observable and Observer?
Java 8 includes java.util.Observable and java.util.Observer. The observer callback is update(Observable o, Object arg); an observable notifies observers through notifyObservers() or notifyObservers(Object). The payload is an Object, so callers must agree on its meaning and may need casts. The subject also inherits from Observable, which can be awkward when it already needs another superclass.
Oracle deprecated both APIs in Java 9. Its documentation calls the event model limited: notification order is unspecified, and changes to state do not correspond one-for-one with notifications. The default Observable implementation may notify in registration order, but subclasses can change the order, provide no guarantee, or notify on separate threads. Do not depend on ordering, thread choice, or exact event counts unless your own code provides those guarantees. Oracle: Observable, Java SE 9; Oracle: Observer, current API.
Choose an alternative based on the requirement
| Approach | Payload and lifecycle | Delivery and fit |
|---|---|---|
| Custom Java 8 observer | Can use a domain-specific type and explicit subscribe/unsubscribe methods. | Define ordering, exceptions, and threading yourself. Suits a simple callback list. |
java.util.Observable and Observer |
Callback receives an Object argument; the subject extends Observable. |
Deprecated in Java 9; ordering and notification-to-state-change correspondence are limited as documented by Oracle. |
| JavaBeans event model | Oracle recommends java.beans for a richer event model. |
Consider it when its event model fits; it is not a drop-in replacement for every observer list. |
java.util.concurrent tools |
Choose data structures and coordination suited to the messaging problem. | Oracle recommends this package for reliable and ordered messaging among threads; select a specific design to meet the needed guarantees. |
Flow |
Reactive-streams-style publisher and subscriber APIs. | Oracle recommends it for reactive-streams-style programming. It is not part of the Java SE 8 API baseline. |
The built-in API status and alternatives are described in Oracle’s Java SE 9 Observable documentation. The right choice depends on whether you need typed events, explicit lifecycle management, ordered or cross-thread delivery, or reactive-streams behavior—not just on which API has the shortest registration call.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

