SEARCH BY

All

  • All
  • Java8
  • Spring

Records in java14 with example

Post a Comment

What is Record

Record is a new feature in Java14 to avoid boilerplate code while using plain java classes like simple and immutable POJO classes which contains variables, setter and getter methods. Each data carrier class(Simple java class) created as Record will extend the class java.lang.Record.

Data carries class or Simple java class has the following
  • Variables
  • Getter and setter methods
  • equal() method
  • hashCode() method
  • toString() method

A class without Record

Following Account class is a plain java class with variables, getter & setter methods, equal(), hashCode() and toString() method.
public class Account {

	int acountNo;
	String bankName;

	public Account(int acountNo, String bankName) {
		super();
		this.acountNo = acountNo;
		this.bankName = bankName;
	}

	public int getAcountNo() {
		return acountNo;
	}

	public void setAcountNo(int acountNo) {
		this.acountNo = acountNo;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + acountNo;
		result = prime * result + ((bankName == null) ? 0 : bankName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (acountNo != other.acountNo)
			return false;
		if (bankName == null) {
			if (other.bankName != null)
				return false;
		} else if (!bankName.equals(other.bankName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Account [acountNo=" + acountNo + ", bankName=" + bankName + "]";
	}

}

Creating a class with Range

 Now we will create the above class(Account.java) by using the keyword record.
public record Account(int acountNo, String bankName) {}
At the compile time java14 supporting java compiler will generate the code with all methods(getter, setter, toString, equal, hashCode).

Adding additional methods in Record class

We can also add extra methods other than getter, setter, toString, equal and hashCode.
public record Account(int acountNo, String bankName) {
public String getBothNumberAndName(){
return accountNo+""+bankName;
}
}

Other Constructors

 We can create constructors also in record class
public record Account(int acountNo, String bankName) {
public Account(int number){
this(number,"SBI");
}
}

Validating with constructor

 we can create compact constructor in record class to validate input data
public record Account(int acountNo, String bankName) {
  public Account {
        if (acountNo < 1) {
            throw new IllegalArgumentException("acountNo must be positive value");
        }
        
    }
}

References

Records come to java https://openjdk.java.net/jeps/384
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox