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()
andgetAge()
). equals()
,hashCode()
, andtoString()
methods, which are based on the record's components.
In Java, a record is a new feature introduced in Java 14 that provides a concise way to declare classes that are primarily used to store data. It is designed to simplify the creation of immutable data objects by automatically generating useful methods such as constructors, getters, equals(), hashCode(), and toString().
Here are some key characteristics and features of Java records:
1. Immutable: Records are implicitly `final` and immutable. Once created, their state cannot be modified.
2. Concise Syntax: Records have a compact syntax for declaring the fields and automatically generating the necessary methods. You only need to declare the fields as the components of the record.
3. Default Constructor: Records automatically generate a constructor that takes all the fields as parameters, allowing you to create instances easily.
4. Generated Methods:
- Getters: Records automatically generate accessor methods for each field, which follow the naming convention fieldName().
- equals() and hashCode(): Records generate these methods, which compare the values of all the fields for equality and calculate the hash code respectively.
- toString(): Records generate a toString() method that provides a string representation of the record, including the names and values of its fields.
5. Inheritance and Interfaces: Records can extend another class and implement interfaces, just like regular classes.
6. Compact Syntax for Local Records: Starting from Java 16, you can define local records within a method or code block, providing a concise way to define data structures for localized use.
Records are particularly useful for data-centric classes, such as DTOs (Data Transfer Objects), POJOs (Plain Old Java Objects), or any class primarily used for storing and transferring data. They promote immutability, encapsulation, and conciseness in Java code.
Here's an example of declaring a simple record representing a point in a 2D coordinate system:public
record Point(int x, int y) {
}
This single-line declaration defines a record named `Point` with two fields `x` and `y`. The record implicitly generates a constructor, getters (`x()` and `y()`), `equals()`, `hashCode()`, and `toString()` methods.
Example 1:
public record Person(String name, int age) {
// Empty body; the record automatically generates constructor, getters, equals(), hashCode(), and toString()
}
Example 2 :
public class RecordExample {
public static void main(String[] args) {
Person person1 = new Person("John", 25);
Person person2 = new Person("Jane", 30);
System.out.println(person1); // Automatically calls toString()
System.out.println(person2);
System.out.println(person1.name()); // Automatically generated getter method
System.out.println(person2.age());
System.out.println(person1.equals(person2)); // Automatically compares the fields
// Records are immutable, so modifying their values creates a new instance
Person modifiedPerson = person1.withAge(30);
System.out.println(modifiedPerson);
}
}
In the example above, the Person
record is defined with two fields: name
of type String
and age
of type int
. The record automatically generates a constructor, getters for the fields (name()
and age()
), an equals()
method that compares the fields, a hashCode()
method, and a toString()
method.
In the main()
method, two Person
objects are created using the constructor, and their values are printed using System.out.println()
. The generated getter methods are used to access individual fields.
The equals()
method is used to compare the two Person
objects, and it returns false
since their name
and age
values differ.
Finally, the withAge()
method, which is automatically generated for records, is used to create a new Person
object with a modified age
field.
Comments
Post a Comment