DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

  1. DZone
  2. Refcards
  3. Java 15
refcard cover
Refcard #337

Java 15

Developers are seeing new features being added to the Java platform more quickly than ever before. Read this Refcard to learn more about the latest features and additions in Java 15, including sealed classes, hidden classes, and more.

Free PDF for Easy Reference
refcard cover

Written By

author avatar Simon Ritter
Deputy CTO, Azul Systems
Table of Contents
► Introduction ► Key Methods and Techniques ► Real World Applications ► Getting Started With Java 15 ► Conclusion
Section 1

Introduction

The six-month release cadence for the JDK has been running for three years now, and it is clear that this is working very well. As developers, we are seeing more new features being added more quickly to the Java platform than ever before.  

Although Java 15 includes fourteen JDK Enhancement Proposals (JEPs), the new features relevant to developers are relatively small except for sealed classes. In fact, four of the JEPs are for the deprecation or removal of features, and two are merely turning experimental features into product features. 


This is a preview of the Java 15 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 2

Key Methods and Techniques

Sealed Classes

The most significant new feature in JDK 15 is the introduction of sealed classes as a preview feature. Preview features first appeared in Java 12 and allowed for new functionality to be included in the JDK without adding it to the Java SE specification (this applies to the Java language, Java Virtual Machine, and Java SE APIs).  

By doing this, feedback from users can be gathered and changes made where necessary or even, if deemed appropriate, the feature could be removed completely. To use a preview feature, it is necessary to enable them both at compile time and runtime using the --enable-preview flag (the --release flag must also be used at compile time).  

Java is an object-oriented language, one of the main features of which is inheritance. The idea is straightforward: Common state and behavior can be abstracted to a super-class and shared (through inheritance) by sub-classes. This eliminates the need to reproduce the instance variables or methods of the super-class in each sub-class.   

Prior to Java 15, the only control a developer had over inheritance was the final modifier.  This prevents any class from extending the one using the modifier. The only other way to control inheritance is to make a class or its constructors’ package-private, limiting possible sub-classes to those in the same package. This approach is sometimes too coarse, which is where sealed classes come in. 

Sealed classes (explained in detail in JEP 360) provide a fine-grained mechanism that allows a developer to restrict which other classes or interfaces may extend them. You can think of final classes as the ultimate sealed class since no other classes can extend them. 

The syntax for sealed classes extends the existing way of defining a class by adding the sealed and permits reserved identifiers. This is important because they are not new keywords, so they can still be used as variable names and not break existing code. 

A sealed class can be defined like this: 

Java
 




x


 
1
  public sealed class Foo permits A, B, C { ... } 


 

Only classes A, B, and C can extend class Foo, and they must be in the same package or module. The permits part of the definition can be omitted if A, B, and C are in the same compilation unit (typically file).  

Each permitted sub-class must have its inheritance capabilities explicitly specified. In our simple example:  

  • Class A could be defined as final so that no further inheritance is allowed. 

  • Class B could be defined as sealed, permitting a closed set of classes to inherit from it in the same ways as Foo does. 

  • Class C could be defined as non-sealed, which reverts it to be open and allows any class to inherit from it. As non-sealed contains a hyphen, it has been made a reserved word in Java (the first with a hyphen). Variable names may not include a hyphen, so there is no impact on backwards compatibility. 

Java
 




xxxxxxxxxx
1


 
1
  public non-sealed class C { ... } 



The Java reflection system has been updated to include sealed classes. The java.lang.Class class has two new methods, isSealed() and permittedSubclasses(which returns an array of ClassDescobjects). 

Although the JEP title is sealed classes, this feature can also be used for the inheritance of interfaces as well. 

Sealed classes are part of Project Amber, the goal of which is to explore and incubate smaller, productivity-oriented Java language features. They will also help another feature under consideration for inclusion in Java: pattern matching for switch. With sealed classes, it will be possible to eliminate the need for a default case and enable the compiler to perform more extensive checks. 

Section 3

Real World Applications

Java is already used in many, many real-world applications.  The developers of the Java platform understand that to continue to maintain its appeal, they must continue to fine-tune the language syntax and the JVM. Developers will always want new features in the platform. The challenge is providing new features without sacrificing the readability of Java (essential to maintaining other people’s code) or breaking compatibility with existing applications. 

All of the evidence shows that the architects of Java are succeeding admirably in these, often orthogonal, objectives.  


This is a preview of the Java 15 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 4

Getting Started With Java 15

Java 15 binaries can be downloaded from a number of providers, including Oracle (through their Oracle OpenJDK distribution) and Azul Systems (through their Zulu Community distribution). 

Although the six-month release cycle for Java makes life challenging for IDE developers, the good news is that IntelliJ from JetBrains already has support for Java 15. NetBeans will have support in version 12.2 scheduled for release in October, so only a few weeks behind the release of JDK 15.  


This is a preview of the Java 15 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 5

Conclusion

Apart from sealed classes, JDK 15 is a minor release in terms of new functionality. However, this is a further demonstration of how well the six-month release cadence for the JDK is working, keeping Java the most popular development platform on the planet. 


This is a preview of the Java 15 Refcard. To read the entire Refcard, please download the PDF from the link above.

Like This Refcard? Read More From DZone

related article thumbnail

DZone Article

A First Look at Records in Java 14
related article thumbnail

DZone Article

Maximize Kubernetes Security: Automate TLS Certificate Management With Cert-Manager on KIND Clusters
related article thumbnail

DZone Article

Combatting the 3 AM Ransomware Menace
related article thumbnail

DZone Article

The Circuit Breaker Pattern: Fortifying Microservices Architecture
related refcard thumbnail

Free DZone Refcard

Introduction to Cloud-Native Java
related refcard thumbnail

Free DZone Refcard

Java 15
related refcard thumbnail

Free DZone Refcard

Java 14
related refcard thumbnail

Free DZone Refcard

Getting Started With Headless CMS

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}
by
DZone Core CORE
· {{ parent.articleDate | date:'MMM. dd, yyyy' }} {{ parent.linkDate | date:'MMM. dd, yyyy' }}
Tweet
{{ parent.views }} ViewsClicks
  • Edit
  • Delete
  • {{ parent.isLocked ? 'Enable' : 'Disable' }} comments
  • {{ parent.isLimited ? 'Remove comment limits' : 'Enable moderated comments' }}