xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// New feature since jdk 14
// BEWARE - IMMUTABLE CLASS, you cannot use setters (Bad thing)
// Alutomatically creates toString, setters and constructors with initialized arguments (Good thing)
// Record.java
public record Record(String name, int age) {}
// Main.java
public class Main {
public static void main(String[] args) {
// Create an IMMUTABLE object of Record using the constructor
Record record = new Record("John", 30);
// Printing the object using toString() method will print all the argument values
System.out.println("Record object: " + record);
// You cannot explicitly set values to fields of a record after initialization
// record.setName("Alice"); // This will cause a compilation error
// You can access the components directly (getters only)
System.out.println("Name: " + record.name());
System.out.println("Age: " + record.age());
}
}
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// In Java, a record is an immutable class that encapsulates a set of related data fields.
// It provides useful methods like constructors, accessors, and equals/hashCode implementations.
// Records are introduced in Java 16.
public record Person(String name, int age) {
// Additional methods, if needed
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Usage of the record
Person person = new Person("John Doe", 25);
person.printDetails();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();
xxxxxxxxxx
// create immutable record added in jdk 14
// provides all-args constructor, getters, equals, hashcode and toString
public record Person (String name, String address) {}
// usage
Person person = new Person("John Doe", "100 Linda Ln.");
// access fields
String name = person.name();
String address = person.address();