SlideShare a Scribd company logo
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

More Related Content

Similar to Record Classes in Java

Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
Mahmoud Ouf
 
Solidity-CheatSheet.pdf
Solidity-CheatSheet.pdfSolidity-CheatSheet.pdf
Solidity-CheatSheet.pdf
Mohankumar975815
 
Lecture09a computer applicationsie1_dratifshahzad
Lecture09a computer applicationsie1_dratifshahzadLecture09a computer applicationsie1_dratifshahzad
Lecture09a computer applicationsie1_dratifshahzad
Atif Shahzad
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
Mahmoud Ouf
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Ducat India
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
Eqela Core API and Utilities
Eqela Core API and UtilitiesEqela Core API and Utilities
Eqela Core API and Utilitiesjobandesther
 
Creating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfCreating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdf
ShaiAlmog1
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
Mahmoud Ouf
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Young Alista
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Luis Goldster
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
James Wong
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Fraboni Ec
 
Encapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclassEncapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclass
Hoang Nguyen
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Harry Potter
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Tony Nguyen
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp Book
guestd9317c
 
Qtp Scripts
Qtp ScriptsQtp Scripts
Qtp Scripts
G.C Reddy
 
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdfptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
jorgeulises3
 

Similar to Record Classes in Java (20)

Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
Solidity-CheatSheet.pdf
Solidity-CheatSheet.pdfSolidity-CheatSheet.pdf
Solidity-CheatSheet.pdf
 
Lecture09a computer applicationsie1_dratifshahzad
Lecture09a computer applicationsie1_dratifshahzadLecture09a computer applicationsie1_dratifshahzad
Lecture09a computer applicationsie1_dratifshahzad
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
 
Eqela Core API and Utilities
Eqela Core API and UtilitiesEqela Core API and Utilities
Eqela Core API and Utilities
 
Creating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfCreating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdf
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Encapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclassEncapsulation getters setters_anonymousclass
Encapsulation getters setters_anonymousclass
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp Book
 
Qtp Scripts
Qtp ScriptsQtp Scripts
Qtp Scripts
 
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdfptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
 

More from Haim Michael

Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
Haim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
Haim Michael
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
Haim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
Haim Michael
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
Haim Michael
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
Haim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
Haim Michael
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
Haim Michael
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]
Haim Michael
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
Haim Michael
 

More from Haim Michael (20)

Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 

Recently uploaded

急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
Himani415946
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
ShahulHameed54211
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
TristanJasperRamos
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 

Recently uploaded (16)

急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
Output determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CCOutput determination SAP S4 HANA SAP SD CC
Output determination SAP S4 HANA SAP SD CC
 
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptxLiving-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
Living-in-IT-era-Module-7-Imaging-and-Design-for-Social-Impact.pptx
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 

Record Classes in Java

  • 1. 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
  • 2. © 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.
  • 3. © 2008 Haim Michael 20230307 What is a Record Class? public record Book(int id, String title, String author) {}
  • 4. © 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.
  • 5. © 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; } }
  • 6. © 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.
  • 7. © 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; } }
  • 8. © 2008 Haim Michael 20230307 The hashCode Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 9. © 2008 Haim Michael 20230307 The toString Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 10. © 2008 Haim Michael 20230307 The equals Method  We can implement it by ourselves. Our implementation will take the place of the auto generated one.
  • 11. © 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.
  • 12. © 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.
  • 13. © 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.
  • 14. © 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.
  • 15. © 2008 Haim Michael 20230307 Final Class  The record class we define is automatically final. We cannot extend a record class.
  • 16. © 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.
  • 17. © 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.
  • 18. © 2008 Haim Michael 20230307 The java.lang.Record Class  Every record class we define implicitly extends the java.lang.Record class.
  • 19. © 2008 Haim Michael 20230307 Questions & Answers Thanks for Your Time! Haim Michael haim.michael@lifemichael.com +972+3+3726013 ext:700 life michae l