In this post, lets look at the structure of a record definition and understand its constituents
A record definition looks like this
Each part of the definition explained below:
- access specifier - Specifies the scope for the record type. This is optional and if specified should be public. When not given, the record is defined in default scope.
- Record keyword - The keyword indicating that this is a record definition
- Name of the record - The Name for the record type defined
- Components of the record - Specifies the list of attributes that comprise this record, each defined with its type. This is optional and a record can be defined without any components. This section is also referred to as the record header
- Body of the record - Block containing the implementation code for the record. It can be left empty to have the default behavior of the record. To add new functionality to the record or to override the default behavior, implementation code should be provided in the body.
Here is a bare minimum definition for a record
record Cat() {}
We touched upon how the record definition gets converted to a class definition with constructor, accessor and other method implementations during compilation in the previous post.
Lets dig a bit deeper into this and see what actually happens in the next post.
Sample code used in this post can be downloaded from https://github.com/ashokkumarta/awesomely-java/tree/main/2021/02/Language-Features/Record/Structure-of-record-definition
No comments:
Post a Comment