Java's Observer and Observable Are Deprecated in JDK 9
Around since Java 1.0, the Observable class will be deprecated in Java 9, as is the Observer interface. See how they've been replaced over time.
Join the DZone community and get the full member experience.Join For Free
In the blog post Applying JDK 9 @Deprecated Enhancements, I discussed additions of the optional elements (methods) forRemoval() and since() to the @Deprecated annotation in JDK 9. I stated in that post, "The application of new JDK 9 @Deprecated methods on the Java SE API can also be instructive in how they are intended to be used." In this post, I look at the application of the enhanced
@Deprecated annotation to the JDK class java.util.Observable.
java.util.Observable has been with us almost since the beginning (since Java 1.0). As of JDK 9, however, it will be marked as deprecated. The following screen snapshot shows a portion of this class's Javadoc representation in a web browser.
This is an example of a class that in the category "Deprecated With No Plans for Removal" described in my previous post. The presence of
since() provides information on when it was deprecated (JDK 9) and the absence of
forRemoval() indicates a lack of concrete plans to actually remove the class. The java.util.Observer interface has also been deprecated in a similar manner and its documentation references the documentation for the
Not only does the
Observable documentation relay when it was deprecated, but it also documents the problems with
Observable that make deprecation desirable and provides important information on alternatives that might be used instead of
This class and the Observer interface have been deprecated. The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package. For reactive streams style programming, see the Flow API.
This is a good example of how Java developers can use the Javadoc tag
@deprecated to provide much deeper detail related to deprecation than can be provided even with the enhanced
@Deprecated annotation. JEP 277 ("Enhanced Deprecation") explicitly listed unifying of Javadoc tag
@deprecated and annotation
@Deprecated as a "non-goal": "It is not a goal of this project to unify the @deprecated Javadoc tag with the @Deprecated annotation."
Additional details justifying the deprecation of
Observer can be found in JDK-8154801 ("deprecate Observer and Observable"). There is a quote in there from Josh Bloch as part of JDK-4180466 ("Why is java.util.Observable class not serializable.") dated February 1999:
This class is no longer under active development. It is largely unused in the JDK, and has, for the most part, been superseded by the 1.1 Beans/AWT event model. ... Observable has fallen into disuse and is no longer under active development.
For the most part, it seems that
Observable are not used much, so deprecation shouldn't be much of an issue, especially given no definitive plans to remove these altogether.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.