Home » Java » Core Java » Candidate JEPs: Records and Sealed Types

About Dustin Marx

Dustin Marx

Candidate JEPs: Records and Sealed Types

Mark Reinhold announced two new closely related candidate JDK Enhancement Proposals (JEPs) on the OpenJDK amber-dev mailing list this week with the posts “New candidate JEP: 359: Records (Preview)” and “New candidate JEP: 360: Sealed Types (Preview).” Both of these candidate JEPs are “preview features” (defined by JEP 12).

JEP 359: Records (Preview)

The JEP 359 “Summary” states, “Enhance the Java programming language with records. Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data.”

The “Motivations and Goals” section of JEP 359 explains how records would benefit Java developers. That section begins by stating that “it is a common complaint that ‘Java is too verbose’ or has too much ‘ceremony” and explaining that “some of the worst offenders are classes that are nothing more than plain ‘data carriers’ that serve as simple aggregates.” This section also states that records are intended to be more than mere “boilerplate reduction” and that they “should be easy, clear, and concise to declare shallowly-immutable, well-behaved nominal data aggregates.” In short, the stated driving goal of JEP 359 is to “model data as data.”

The recently proposed java.lang.Record draft specification provides significant insight into the characteristics of records. The opening paragraph of the “Description” section of JEP 359 also describes records: “Records are a new kind of type declaration in the Java language. Like an enum, a record is a restricted form of class. It declares its representation, and commits to an API that matches that representation. Records give up a freedom that classes usually enjoy: the ability to decouple API from representation. In return, records gain a significant degree of concision.” There is significantly more text in the “Description” section of JEP 359.

JEP 360: Sealed Types (Preview)

The “Summary” section of JEP 360 states, “Enhance the Java programming language with sealed types. Sealed types are classes or interfaces that impose restrictions on which other classes or interfaces may extend or implement them.”

The “Goals” section of JEP 360 is also concise, “Enable classes and interfaces to limit permitted subtypes to an enumerated set of types in the same maintenance domain as the type itself.”

It is the “Description” section of JEP 360 that provides concreteness to JEP 360. That section begins, “A sealed type is one for which subtyping is restricted according to guidance specified with the type’s declaration.” The second paragraph of the “Description” section states that “sealing serves two distinct purposes” and describes those purposes:

  1. “Restricts which classes may be a subclass of a sealed class.”
  2. “Potentially enables exhaustiveness analysis at the use site, such as when switching over type patterns for an instance of a sealed type.”

There are other interesting characteristics of sealed types described in the “Description” section. Some of these that stood out to me are:

  • Use (with example) of the sealed modifier and permits clause.
  • “Abstract subtypes of sealed types are implicitly sealed, unless declared with the non-sealed modifier.”
  • “Concrete subtypes of sealed types are implicitly final, unless declared with the non-sealed modifier.”
  • “Sealing, like finality, is enforced by both the language compiler and by the JVM. The sealed-ness of a type, and its list of permitted subtypes, are reified in the class file and enforced at runtime.”

Other interesting details related to sealed types that are covered in this JEP include restrictions (compiler errors that can occur), the class form for sealed types, and addition of reflection methods to support sealed types.

Conclusion

JEP 359 (Records Preview) and JEP 360 (Sealed Types Preview) reference each other in their documentation. Of the relationship between these two candidate JEPs, JEP 360 states, “Sealed types and records, taken together, form a construct often referred to as algebraic data types.” Records and sealed types are key pieces in the move toward Java support for pattern matching.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Candidate JEPs: Records and Sealed Types

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of