Records in Java

Looking to get certified as a Java SE 11 Developer with Oracle?

Check out this highly rated Udemy course by the Skilltoz team with excellent practice questions that will help you prepare for the Java Certification exam.

For sample Questions, you can also refer to http://talks.skilltoz.com/java-11-certification-exam-questions/

What are Records in Java?

Records (Introduced in Java 14) are restricted forms of classes that are intended to hold immutable data, constructors and accessor methods. They are the best way to design data transfer objects in Java.

For example, consider a class Square defined as below.

final class Square {
	final double side;
	public Square(double side) {
		this.side = side;
	}

	double side() {
		return side;
	}
}

The above class has a field ‘side’ that cannot be modified, a constructor to initialize this field and a getter method that returns the value of the field.

This can be replaced with a record, defined as follows. 

record Square(double side) {
}

What are the members of a record?

The following members are automatically added when a record is created. 

  • A private final field with the same name and declared type as each record component.
  • A public accessor method with the same name and type of the component; in the Square record example, this method is Square::side()
  • A canonical constructor whose signature is the same as the header.
  • Implementations of the equals() and hashCode() methods
  • Implementation of the toString() method that includes the string representation of all the record components, with their names

What is a Canonical Constructor?

A record’s canonical constructor is a constructor that has a parameter list that exactly matches the declared fields of the record. The below example shows a canonical constructor for the record Square.

record Square(Integer side) {
	Square(Integer side) {
		java.util.Objects.requireNonNull(side);
		this.side = side;
	}
}

What is a Compact Constructor?

A record’s compact constructor is a constructor whose signature is implicit (derived from the components automatically). For example, the following compact constructor declaration can replace the earlier example.

record Square(Integer side) {
	Square {
		java.util.Objects.requireNonNull(side);
		}
	}
}

The implicit formal parameters of the compact constructor are assigned to the record class’s private fields automatically. Explicitly assigning record fields (as done in the previous example) is illegal in a compact constructor.

Can a record be extended?

Records are implicitly final, hence they cannot be extended. Also every record extends java.lang.Record and hence they cannot extend any other classes.

Can records have static members?

Yes, it is possible to define static fields, methods and initializers in records.

Java 17 Certification Practice Questions on Records

For answers, visit here.

Q1. Given the following code. 

public class Record1 {
	public static void main(String[] args) {
		Student student1 = new Student("Tom", "Harris");
		Student student2 = new Student("Tom", "Harris");
	}
}

Which of the following will print true?

A. System.out.println(student1.equals(student2));
B. System.out.println(student1.hashCode()==student2.hashCode());
C. System.out.println(student1.name.equals(student2.name));
D. System.out.println(student1==student2);

Q2. Given the following code. 

record Student(String name, String address) {
}

Which of the following is legal?

A. Student student1 = new Student();
   System.out.println(student1.name);
B. Student student1 = new Student("Tina","Texas");
   System.out.println(student1.name());
C. Student student1 = new Student("Tom", "Harris");
   student1.name("Mary");
D. Student student1 = new Student();
   student1.name("Mary");

Q3. What will be the result?

record Square(Integer side) {
	Square { // Line 1
		java.util.Objects.requireNonNull(side); // Line 2
		this.side = side; // Line 3
	}
}

public class Record2 {
	public static void main(String[] args) {
		Square s = new Square(0);
		System.out.println(s.side());
	}
}

A. Compiler error at line 1
B. Compiler error at line 2
C. Compiler error at line 3
D. Exception at runtime
E. Prints 0

References

For answers to the above questions with detailed explanations, visit here.

Read about the Record type from the Oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *