Record is one of the cool features Java has and below are the difference with normal class Vs Record class and how it will help us to get rid of boilerplate code.
For understanding purpose, we create a simple class with two variables, name and email.
you can see how much code we need to write in legacy class to deal with just to deal with a record/object with two variables
- two variables
- Getters and setter
- Constructor
- equals and hashcode
- toString() Method
In Record
just the class name with variables inside parenthesis
Record
variables are final - so no setters
Record will create a canonocal constructor - with parametrs you have given, where in class it will create only default constructor without paramters
Can have instance methods.
Can have static methods
Static variables, cannot create instance variables - why , by default, Record is a final
Get the value of variables using the name and paranthesis eg:varibaleName()
Record has by default toString method
Cannot extend any class cause By default it extends Record class, But you can implement any interface
Has a compact constructor
Can have validations here
name of the record is enough to declare constructor with "public" access
eg:
public record SampleRecord(String name, String email) {
public SampleRecord {
if (name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank.");
}
}
}
Below are sample code blocks
package com.example.demo.records;
import java.util.Objects;
public class SampleClass {
private String name;
private String email;
public SampleClass(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "SampleClass{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SampleClass that = (SampleClass) o;
return name.equals(that.name) && email.equals(that.email);
}
@Override
public int hashCode() {
return Objects.hash(name, email);
}
}
package com.example.demo.records;
public record SampleRecord(String name, String email) {
public static String STATIC_VARIABLE = "static_variable";
// Instance variables are not allowed as by default variables are final cause of Record
//public String instanceVarible ="instanceVairableNot Allowed";
public String instanceMethod() {
return name();
}
public static void staticMethod() {
System.out.println("From static method");
}
public SampleRecord {
if (name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank.");
}
}
}
package com.example.demo.records;
public class TestSampleRecord {
public static void main(String[] args) {
SampleClass sampleClass = new SampleClass("Roshan", "roshan@email.com");
System.out.println(sampleClass);
SampleRecord sampleRecord = new SampleRecord("Mahanama", "mahanama@email.com");
System.out.println("default toString method in Record");
System.out.println(sampleRecord);
System.out.println("From instance method :" + sampleRecord.instanceMethod());
SampleRecord.staticMethod();
System.out.println(new SampleRecord("", ""));
}
}
Output
SampleClass{name='Roshan', email='roshan@email.com'}
default toString method in Record
SampleRecord[name=Mahanama, email=mahanama@email.com]
From instance method :Mahanama
From static method
Exception in thread "main" java.lang.IllegalArgumentException: Name cannot be blank.
at com.example.demo.records.SampleRecord.<init>(SampleRecord.java:16)
at com.example.demo.records.TestSampleRecord.main(TestSampleRecord.java:13)
No comments:
Post a Comment