Java 16 – Record

Java 16 - Record

This topic is about Java 16 – Record.

Java 14 introduces a new class type record as preview feature to facilitate creation of immutable data objects. Java 15 enhances record type further. With Java 16, record is now a standard feature of JDK.

Consider the following example −

ApiTester.java

Example

public class APITester {
   public static void main(String[] args) {
      StudentRecord student = new StudentRecord (1, "Julie", "Red", "VI", 12);
      System.out.println(student.id());
      System.out.println(student.name());
      System.out.println(student);
   } 
}
record StudentRecord(int id, 
   String name, 
   String section, 
   String className,
   int age){}

Compile and Run the program

$javac APITester.java
$java APITester

Output

1
Julie
StudentRecord[id=1, name=Julie, section=Red, className=VI, age=12]

In this topic we learned about Java 16 – Record. To know more, Click Here.

Leave a Reply