{"id":1487,"date":"2021-09-28T13:51:42","date_gmt":"2021-09-28T00:51:42","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1487"},"modified":"2021-10-06T19:47:35","modified_gmt":"2021-10-06T06:47:35","slug":"record-class","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1487","title":{"rendered":"Record Class"},"content":{"rendered":"<p>A record class is a restricted form of classes that models data as data. Thus, also can be thought as data only class. Additionally, this type of class is immutable.<\/p>\n<p>It automatically implements the following methods:<\/p>\n<ul>\n<li>public boolean equals...<\/li>\n<li>public int hashCode...<\/li>\n<li>public String toString() {...}<\/li>\n<\/ul>\n<blockquote>\n<p>The class generated by the record is a final class.<\/p>\n<\/blockquote>\n<p>The constructor will also be handled and the parameters is matching the arrangement of the record parameter<em>(s)<\/em>.<\/p>\n<h2>Use Cases<\/h2>\n<ul>\n<li>Data Transfer Objects (DTO) (i.e. not applicable to JPA entities)<\/li>\n<li>Compound map keys<\/li>\n<li>Multiple return values<\/li>\n<\/ul>\n<h2>Syntax<\/h2>\n<pre><code>record &lt;IDENTIFIER&gt;([[[&lt;FIELD_TYPE_1&gt; &lt;FIELD_NAME_1&gt;][, &lt;FIELD_TYPE_2&gt; &lt;FIELD_NAME_2&gt;]][,... &lt;FIELD_TYPE_2&gt; &lt;FIELD_NAME_N&gt;]]) {}<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>Token<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>IDENTIFIER<\/td>\n<td>A valid Java identifier.<\/td>\n<\/tr>\n<tr>\n<td>FIELD_TYPE_1<br \/>FIELD_TYPE_2<br \/>FIELD_TYPE_N<\/td>\n<td>The type of the field(s) in the record parameter(s).<\/td>\n<\/tr>\n<tr>\n<td>FIELD_NAME_1<br \/>FIELD_NAME_2<br \/>FIELD_NAME_N<\/td>\n<td>The name of the field(s) in the record parameter(s).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>The <strong>identifier<\/strong> has an equivalent <strong>cannonical constructor<\/strong> with parameters equivalent to its parameters.<\/p>\n<p>Each <strong>fields<\/strong> has the following:<\/p>\n<ul>\n<li><strong>private final<\/strong> field of the same name.<\/li>\n<li><strong>public<\/strong> accessor method of the same name and type.<\/li>\n<\/ul>\n<\/blockquote>\n<h2>Example<\/h2>\n<pre><code class=\"language-java\">record Person(String firstName, String lastName) {}<\/code><\/pre>\n<p>The above example is equivalent to the following class definition:<\/p>\n<pre><code class=\"language-java\">public final class PersonC {\n\n    private final String firstName;\n    private final String lastName;\n\n    public PersonC(String firstName, String lastName) {\n        this.firstName = firstName;\n        this.lastName = lastName;\n    }\n\n    public String firstName() {\n        return this.firstName;\n    }\n\n    public String lastName() {\n        return this.lastName;\n    }\n\n    public boolean equals...\n    public int hasCode...\n    public String toString() {...}\n\n}<\/code><\/pre>\n<h2>Compact Constructor<\/h2>\n<p>In the record definition we can use a compact constructor. This constructor is the shorthand form of the generated default constructor. All of the fields indicated on the record parameters are implicitly available in it.<\/p>\n<h3>Syntax of the Compact Constructor<\/h3>\n<pre><code>pubic &lt;IDENTIFIER&gt; {\n    \/\/ Initilization logic.\n}<\/code><\/pre>\n<p>The IDENTIFIER here is matching the record IDENTIFIER, just like when you are writing a constructor of a class but without parameters even the parentheses.<\/p>\n<h3>Example Usage<\/h3>\n<pre><code class=\"language-java\">record Person(String firstName, String lastName) {\n    public Person {\n        java.util.Objects.requireNonNull(firstName);\n        java.util.Objects.requireNonNull(lastName);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A record class is a restricted form of classes that models data as data. Thus, also can be thought as data only class. Additionally, this type of class is immutable. It automatically implements the following methods: public boolean equals&#8230; public int hashCode&#8230; public String toString() {&#8230;} The class generated by the record is a final [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[70,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1487"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1487"}],"version-history":[{"count":5,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1487\/revisions"}],"predecessor-version":[{"id":1494,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1487\/revisions\/1494"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}