Posts

Showing posts from June, 2023

Java Record : new way to create a class

Records were introduced in Java 14. They were added as a preview feature in Java 14 and became a standard feature starting from Java 16.  In Java 14, records were available as a preview feature, which means they were not considered final and could undergo changes based on developer feedback. However, the concept and basic functionality of records remained the same when they became a standard feature in Java 16. Record is class like construct. public record Person (String name, int age) { // You can also add custom methods here if needed } In the example above, we've defined a Person record with two components: name and age . The record keyword automatically generates the following for you: A constructor that takes arguments for all the components. Getter methods for each component (e.g., getName() and getAge() ). equals() , hashCode() , and toString() methods, which are based on the record's components. In Java, a record is a new feature in...