English 中文(简体)
Java 16 - Record
  • 时间:2024-12-22

Java 16 - Record


Previous Page Next Page  

Java 14 introduces a new class type record as preview feature to faciptate 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


pubpc class APITester {
   pubpc static void main(String[] args) {
      StudentRecord student = new StudentRecord (1, "Jupe", "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
Jupe
StudentRecord[id=1, name=Jupe, section=Red, className=VI, age=12]
Advertisements