Welcome to the first article in my series on preparing for the Java 21 certification.
Over the next several posts, we’ll walk through the most important language changes, APIs, and tricky details you’ll need to know for the exam.
In this article, we’ll focus on features from Project Amber — a collection of enhancements designed to make Java more concise, readable, and powerful. Specifically, we’ll look at sealed types, records, pattern matching, and the modern switch expression.
To make things practical, we’ll analyze a question in the style of the certification exam.
❓ Question
What is the answer to this code?
public class Question1 {
sealed interface Animal {
private int age = 0; // LINE 1
void sound();
}
final class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}
record Cat(String name) implements Animal { // LINE 2
public void sound() {
System.out.println("Meow " + name);
}
}
non-sealed class Bird implements Animal { // LINE 3
String type = "Parrot";
public void sound() {
System.out.println("Chirp");
}
}
record Lion(int age) implements Animal {
String name = "Lion"; // LINE 4
@Override
public void sound() {
System.out.println("Roar");
}
}
class Sparrow extends Bird {
// inherits sound()
}
public static void main(String[] args) {
Animal a = new Cat("Kitty");
String result = switch (a) { // LINE 5
case Cat(var n) -> "Cat name: " + n; // LINE 6
case Dog d -> "Dog says woof";
case Sparrow s -> "Tiny bird";
case Bird bird -> "Bird";
case Lion(var age) when age > 1 -> "Lion age: " + age; // LINE 7
case Lion(var age) when age <= 1 -> "Lion age: " + age; // LINE 8
case null -> "null";
};
System.out.println(result);
}
}
Mark 3 options: Which lines will NOT compile?
A) LINE 1
B) LINE 2
C) LINE 3
D) LINE 4
E) LINE 5
F) LINE 6
G) LINE 7
H) LINE 8
🧠 Breaking Down the Errors
1. Sealed Interfaces and Fields (LINE 1)
sealed interface Animal {
private int age = 0; // ❌ ERROR
}
Problem: Interfaces cannot have instance fields.
They may only declare
public static final
constants.✅ Fix:
int AGE = 0; // implicitly public static final
2. Records Implementing Sealed Interfaces (LINE 2)
record Cat(String name) implements Animal { ... }
Valid! Records can implement sealed interfaces.
They automatically generate constructors,
toString
,equals
, andhashCode
.No issues here.
3. Non-Sealed Classes (LINE 3)
non-sealed class Bird implements Animal { ... }
Valid!
A
non-sealed
class breaks the restriction, meaning anyone can subclass it.For example,
Sparrow extends Bird
compiles fine.
4. Records with Extra Fields (LINE 4)
record Lion(int age) implements Animal {
String name = "Lion"; // ❌ ERROR
}
Problem: Records cannot declare additional instance fields.
Records are designed to be immutable data carriers; extra fields must be
static
.✅ Fix:
static String name = "Lion";
5. Switch Expressions and Exhaustiveness (LINE 5)
String result = switch (a) { ... };
Problem: A
switch
over a sealed type must be exhaustive.Since
Animal
is a sealed interface, the compiler requires theswitch
to handle all possible implementations:✅
Dog
— covered✅
Cat
— covered✅
Bird
(and its subtypeSparrow
) — covered⚠️
Lion
— not fully covered
Why? Because both cases for
Lion
use guards (when age > 1
andwhen age <= 1
). The compiler treats these as potentially non-exhaustive, since it cannot prove that everyLion
will match one of the guarded patterns.Without a
default
, this fails.✅ Fix: add a
default
branch:
default -> "Unknown animal";
6. Record Pattern Matching with Guards (LINES 7–8)
case Lion(var age) when age > 1 -> ... case Lion(var age) when age <= 1 -> ...
✅ This is valid.
Record pattern matching deconstructs
Lion(int age)
into its components.when
guards add conditions for finer-grained control.
✅ Correct Answer
A) LINE 1
D) LINE 4
E) LINE 5
Did you get it right? Don’t worry if you missed some; this is a hard question. The goal here is to expose you to several tricky details that often show up in the Java 21 certification exam.
👉 This is just the beginning! In the next articles of this series, we’ll explore more features covered in the certification.
📬 Stay tuned:
If you don’t want to miss the upcoming parts of this Java 21 Certification series, hit the Subscribe button below.