Java 15 Tutorial
Java Other Versions Tutorials
Java 15 Useful Resources
Selected Reading
- Java 15 - Deprecation & Removals
- Java 15 - Other Enhancements
- Java 15 - Deprecation & Removals
- Java 15 - Other Changes
- Java 15 - Garbage Collectors
- Java 15 - Hidden Classes
- Java 15 - record & Sealed Classes
- Java 15 - record
- Java 15 - Text Blocks
- Java 15 - Pattern for instanceOf
- Java 15 - Sealed Classes
- Java 15 - Environment Setup
- Java 15 - Overview
- Java 15 - Home
Java Other Versions Tutorials
- Java 14 Tutorial
- Java 13 Tutorial
- Java 12 Tutorial
- Java 11 Tutorial
- Java 10 Tutorial
- Java 9 Tutorial
- Java 8 Tutorial
- Java Tutorial
Java 15 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 15 - record
Java 15 - Record
Java 14 introduces a new class type record as preview feature to faciptate creation of immutable data objects. Java 15 enhances record type further. It is still a preview feature.
Record object have imppcit constructor with all the parameters as field variables.
Record object have imppcit field getter methods for each field variables.
Record object have imppcit field setter methods for each field variables.
Record object have imppcit sensible implementation of hashCode(), equals() and toString() methods.
With Java 15, native methods cannot be declared in records.
With Java 15, imppcit fields of record are not final and modification using reflection will throw IllegalAccessException.
Example
Consider the following example −
ApiTester.java
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 -Xpnt:preview --enable-preview -source 15 APITester.java $java --enable-preview APITester
Output
1 Jupe StudentRecord[id=1, name=Jupe, section=Red, className=VI, age=12]Advertisements