Nearby lessons

101 of 125

Java - Records

📌 What You Will Learn
  • Switch expressions — now FINAL
  • Records — the compact class (preview)
  • Pattern matching for instanceof (preview)
  • Helpful NullPointerException messages
  • Text blocks second preview

Records is a core concept of the Java language. This lesson explains Records — Classes Made Simple (Preview) and Java 17 — Sealed Classes with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Records — Classes Made Simple (Preview)

Writing a simple data class in old Java is very long — fields, constructor, getters, toString, equals, hashCode... Records (previewed here, final in Java 16) do all of that automatically with one line.

Example01
JCode Cell
1// OLD - a simple data class needs all this boilerplate
2class Student {
3 private final int rollNo;
4 private final String name;
5 Student(int r, String n) { rollNo = r; name = n; }
6 int rollNo() { return rollNo; }
7 String name() { return name; }
8 public String toString() { return "Student[" + rollNo + ", " + name + "]"; }
9 // ... plus equals() and hashCode() ...
10}

Records — Classes Made Simple (Preview)

A record automatically gives you: the constructor, the accessor methods (named like rollNo(), without 'get'), and toString, equals, hashCode. Records are meant for classes that are just data carriers — perfect for DTOs (Data Transfer Objects).

In simple words: A record is a class that writes all the boring boilerplate for you. You just list the fields in parentheses, and Java creates the constructor, accessors, toString, equals, and hashCode automatically.
Example02
JCode Cell
1// NEW - a record gives all of that automatically
2record Student(int rollNo, String name) { }
3 
4class Demo {
5 public static void main(String[] args) {
6 Student s = new Student(101, "Rahul");
7 System.out.println(s.rollNo()); // 101
8 System.out.println(s.name()); // Rahul
9 System.out.println(s); // auto toString
10 }
11}
Output
101 Rahul Student[rollNo=101, name=Rahul]

Java 17 — Sealed Classes

A sealed class controls exactly which classes may extend it. It sits between open to everyone (normal) and closed to everyone (final):

Why useful? It makes a closed family of types, so switch statements can cover all cases safely (the compiler knows the full list).

In simple words: A sealed class is a family of classes with a fixed member list. Only the classes named in permits can extend it, so the compiler always knows every possible subclass.
Example03
JCode Cell
1sealed class Shape permits Circle, Square, Triangle { }
2final class Circle extends Shape { }
3final class Square extends Shape { }
4final class Triangle extends Shape { }
5 
6// any other class trying to extend Shape -> compile error
📝 Key Takeaways
  • Switch expressions became FINAL in Java 14.
  • Records create data classes with automatic constructor, getters, toString, equals, hashCode.
  • Pattern matching for instanceof combines check and cast into one step.
  • Helpful NPE messages tell you exactly which value was null.
  • Text blocks got a second preview before becoming final in Java 15.
  • After Java 14: Java 15-24 added text blocks, records, sealed classes, virtual threads.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2