Record Classes in Java
Haim Michael
March 7th
, 2023
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
© 2008 Haim Michael 20230307
What is a Record Class?
 When declaring a record class we declare a sequence of
fields. The equals, hashCode, and toString are
automatically generated. Each field we declare automatically
becomes a final private field. In order to allow us getting
the values of those fields, for each field a method with the
same name is auto generated. In addition, a simple
constructor is automatically generated in accordance with the
declared fields.
© 2008 Haim Michael 20230307
What is a Record Class?
public record Book(int id, String title, String author) {}
© 2008 Haim Michael 20230307
Simple Data Carrier
 The purpose of the record class is to serve as a simple data
carrier. For that reason, the fields automatically become final.
© 2008 Haim Michael 20230307
Explicit Constructor
 When declaring a record we can declare a constructor that
will take the place of auto generated one.
public record Book(int id, String title, String author)
{
public Book(int id, String title, String author)
{
this.id = id;
this.title = title;
this.author = author;
}
}
© 2008 Haim Michael 20230307
Explicit Constructor
 When declaring a constructor we can avoid the tedious repeat
of all components.
public record Book(int id, String title, String author) {
public Book {
if(id<0)
throw new UnsupportedOperationException(
"id cannot be negative");
}
}
The assignment of the arguments take place
automatically. Referring the this keyword is
impossible.
© 2008 Haim Michael 20230307
Implementing The Accessors
 We can implement the access methods by ourselves. We
should make sure the type of their returned value is the same
and we should make sure they are with the same
accessibility.
public record Book(int id, String title, String author) {
public int id() {
System.out.println(“id was retrieved”);
return id;
}
}
© 2008 Haim Michael 20230307
The hashCode Method
 We can implement it by ourselves. Our implementation will
take the place of the auto generated one.
© 2008 Haim Michael 20230307
The toString Method
 We can implement it by ourselves. Our implementation will
take the place of the auto generated one.
© 2008 Haim Michael 20230307
The equals Method
 We can implement it by ourselves. Our implementation will
take the place of the auto generated one.
© 2008 Haim Michael 20230307
Static Members
 We can add static members to the record class we declare.
We can add static variables, static methods, and static blocks.
© 2008 Haim Michael 20230307
Instance Members
 We cannot add instance variables to the definition of our new
record class. It is certainly possible to add the definition of
instance methods.
© 2008 Haim Michael 20230307
Nested Classes
 We can include in our record the definition of nested classes,
nested interfaces, and nested records. The new nested types
cannot be instance members.
© 2008 Haim Michael 20230307
Native Methods
 It isn't possible to include within the definition of a new record
the definition of a native method.
© 2008 Haim Michael 20230307
Final Class
 The record class we define is automatically final. We cannot
extend a record class.
© 2008 Haim Michael 20230307
Local Record Class
 When we define a record class within the scope of a method
we get a local record class.
© 2008 Haim Michael 20230307
Serialization
 We can serialize and deserialize instances of record classes.
We cannot customize the serialization by providing our
versions for methods, such as writeObject, readObject,
and readObjectNoData.
© 2008 Haim Michael 20230307
The java.lang.Record Class
 Every record class we define implicitly extends the
java.lang.Record class.
© 2008 Haim Michael 20230307
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
life
michae
l

Record Classes in Java

  • 1.
    Record Classes inJava Haim Michael March 7th , 2023 All logos, trade marks and brand names used in this presentation belong to the respective owners. life michae l
  • 2.
    © 2008 HaimMichael 20230307 What is a Record Class?  When declaring a record class we declare a sequence of fields. The equals, hashCode, and toString are automatically generated. Each field we declare automatically becomes a final private field. In order to allow us getting the values of those fields, for each field a method with the same name is auto generated. In addition, a simple constructor is automatically generated in accordance with the declared fields.
  • 3.
    © 2008 HaimMichael 20230307 What is a Record Class? public record Book(int id, String title, String author) {}
  • 4.
    © 2008 HaimMichael 20230307 Simple Data Carrier  The purpose of the record class is to serve as a simple data carrier. For that reason, the fields automatically become final.
  • 5.
    © 2008 HaimMichael 20230307 Explicit Constructor  When declaring a record we can declare a constructor that will take the place of auto generated one. public record Book(int id, String title, String author) { public Book(int id, String title, String author) { this.id = id; this.title = title; this.author = author; } }
  • 6.
    © 2008 HaimMichael 20230307 Explicit Constructor  When declaring a constructor we can avoid the tedious repeat of all components. public record Book(int id, String title, String author) { public Book { if(id<0) throw new UnsupportedOperationException( "id cannot be negative"); } } The assignment of the arguments take place automatically. Referring the this keyword is impossible.
  • 7.
    © 2008 HaimMichael 20230307 Implementing The Accessors  We can implement the access methods by ourselves. We should make sure the type of their returned value is the same and we should make sure they are with the same accessibility. public record Book(int id, String title, String author) { public int id() { System.out.println(“id was retrieved”); return id; } }
  • 8.
    © 2008 HaimMichael 20230307 The hashCode Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 9.
    © 2008 HaimMichael 20230307 The toString Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 10.
    © 2008 HaimMichael 20230307 The equals Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 11.
    © 2008 HaimMichael 20230307 Static Members  We can add static members to the record class we declare. We can add static variables, static methods, and static blocks.
  • 12.
    © 2008 HaimMichael 20230307 Instance Members  We cannot add instance variables to the definition of our new record class. It is certainly possible to add the definition of instance methods.
  • 13.
    © 2008 HaimMichael 20230307 Nested Classes  We can include in our record the definition of nested classes, nested interfaces, and nested records. The new nested types cannot be instance members.
  • 14.
    © 2008 HaimMichael 20230307 Native Methods  It isn't possible to include within the definition of a new record the definition of a native method.
  • 15.
    © 2008 HaimMichael 20230307 Final Class  The record class we define is automatically final. We cannot extend a record class.
  • 16.
    © 2008 HaimMichael 20230307 Local Record Class  When we define a record class within the scope of a method we get a local record class.
  • 17.
    © 2008 HaimMichael 20230307 Serialization  We can serialize and deserialize instances of record classes. We cannot customize the serialization by providing our versions for methods, such as writeObject, readObject, and readObjectNoData.
  • 18.
    © 2008 HaimMichael 20230307 The java.lang.Record Class  Every record class we define implicitly extends the java.lang.Record class.
  • 19.
    © 2008 HaimMichael 20230307 Questions & Answers Thanks for Your Time! Haim Michael haim.michael@lifemichael.com +972+3+3726013 ext:700 life michae l