Record in Java
On this page (4sections)
Hey Java Buddies, Do you know the record in Java?
π₯ In Java, the record is identical to the class that acts as a carrier for immutable data. π₯
π As per the documentation, records are described as classes that act as transparent carriers for immutable data.Β
Behaviours and Feature On Java Records
β Help developers to focus on designing immutable data than complex behavior.
β Automatically implement data-driven methods such as equals and accessors.
β Reduce class boilerplate for the immutable classes.
β Design an object-oriented data model construction that describes a simple aggregation of values.
β Maintain long-standing Java principles such as nominal typing and migration compatibility.
β Complete a probable gap in Javaβs type system. Provide language-level syntax for a typical programming pattern
Limitations on Java Records
β A record class declaration doesnβt have an extended clause. Its superclass is java.lang.record and cannot inherit from another class.Β
β A record is final. That means that the state of a record can not be enhanced later by another class using inheritance.
β It is immutable.
β Instance fields are not allowed in a record class.Β
Note:
π Records were suggested and delivered in JDK 14 as a preview feature. Records features were finalized in JDK 16.
Tags:
#java #newfeatures #jdk #records #data #programming #design #language