Free AI web copilot to create summaries, insights and extended knowledge, download it at here
5511
Abstract
s-keyword">class</span> <span class="hljs-title class_">FiveFivesJava</span> {
List<String> fiveFives = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span><>();
<span class="hljs-comment">// Initializer block, see </span>
<span class="hljs-comment">// https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html</span>
{
<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <= <span class="hljs-number">4</span>; i++) {
fiveFives.add(<span class="hljs-string">"Fives"</span> + i);
}
}
}</pre></div><p id="56dd">The only thing that’s new in Kotlin is the header of the primary constructor (arguments and optional modifiers/annotations). For more information on the execution order in Java, as well as an explanation of why you should make absolutely sure you only call private or final methods during construction, <a href="https://lustforge.com/2014/02/08/dont-call-non-final-methods-from-your-constructor-please/">see here.</a></p><p id="8c84">In Kotlin, you can (and should) go one step further.</p><p id="a1a8">Notice that the only purpose of the <code>integer</code> argument was to be immediately assigned to a property. If only we could somehow say "this class has <i>these</i> properties, and expects their values to be specified as constructor arguments".</p><p id="c2a9">As it turns out, in Kotlin, we can:</p>
<figure id="9ea5">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fd6iYkPPTx&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2Fd6iYkPPTx&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="4926">Awesome!</p><p id="cab5">Remember, primary constructor arguments behave like any other arguments, and all the features that apply to normal function arguments (such as default values, named parameters, etc.) apply to primary constructor arguments as well.</p>
<figure id="3a3c">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FV6eICDZaf&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FV6eICDZaf&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="35e3">Secondary constructors</h1><p id="1c71">Classes can also declare secondary constructors, which are prefixed with <code>constructor</code>. Every secondary constructor must call the primary constructor, either directly or through another secondary constructor. This is done immediately via the <code>this()</code> keyword (a matching constructor is selected based on the signature).</p>
<figure id="43e0">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FtCZ-hFjWH&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FtCZ-hFjWH&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="088c">Exercises</h1><p id="5797">The following class represents an immutable <code>Person</code> object. Since we haven’t discussed properties in Kotlin yet, we purposefully avoid the usage of getters and setters for now, and use <code>public</code> <code>final</code> fields. We wish to be able to easily construct <code>Person</code>s with any combination of the fields (i.e. only <code>title</code> + <code>lastName</code>, or <code>firstName</code> + <code>middleName</code> + <code>lastName</code>, etc.). We solve this by adding a builder class.</p><p id="2b3e">We also want to simplify the object construction for certain “common cases” e.g. we want to be able to write <code>new Person(“Harry”, “Potter”)</code> instead of having to write</p><div id="7e14"><pre><span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>.Builder()
.setFirstName(“Harry”)
.setLastName(“Potter”)
.buildPerson()</pre></div><p id="e35f">As “common cases”, we (randomly) pick “<code>title</code> + <code>firstName</code> + <code>middleName</code> + <code>lastName</code>” and “<code>firstName</code> + <code>lastName</code>”. We would also like to include the combination “<code>title</code> + <code>lastName</code>” (e.g. Mr. Bean), but in this case, we have no option but to use the builder.</p><p id="2672">The class implementation follows. Your task is to p
Options
rovide the same functionality in Kotlin, so you experience firsthand how much simpler things get.</p><div id="8ac6"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonJava</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> String title;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> String firstName;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> String middleName;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> String lastName;
<span class="hljs-keyword">public</span> <span class="hljs-title function_">Person</span><span class="hljs-params">(
String title,
String firstName,
String middleName,
String lastName
)</span> {
<span class="hljs-built_in">this</span>.title = title;
<span class="hljs-built_in">this</span>.firstName = firstName;
<span class="hljs-built_in">this</span>.middleName = middleName;
<span class="hljs-built_in">this</span>.lastName = lastName;
}
<span class="hljs-keyword">public</span> <span class="hljs-title function_">Person</span><span class="hljs-params">(
String firstName,
String lastName
)</span> {
<span class="hljs-built_in">this</span>(<span class="hljs-string">""</span>, firstName, <span class="hljs-string">""</span>, lastName);
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Builder</span> {
<span class="hljs-keyword">private</span> <span class="hljs-type">String</span> <span class="hljs-variable">title</span> <span class="hljs-operator">=</span> <span class="hljs-string">""</span>;
<span class="hljs-keyword">private</span> <span class="hljs-type">String</span> <span class="hljs-variable">firstName</span> <span class="hljs-operator">=</span> <span class="hljs-string">""</span>;
<span class="hljs-keyword">private</span> <span class="hljs-type">String</span> <span class="hljs-variable">middleName</span> <span class="hljs-operator">=</span> <span class="hljs-string">""</span>;
<span class="hljs-keyword">private</span> <span class="hljs-type">String</span> <span class="hljs-variable">lastName</span> <span class="hljs-operator">=</span> <span class="hljs-string">""</span>;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setTitle</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String newTitle)</span> {
title = newTitle;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setFirstName</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String newFirstName)</span> {
firstName = newFirstName;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setMiddleName</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String newMiddleName)</span> {
middleName = newMiddleName;
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setLastName</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String newLastName)</span> {
lastName = newLastName;
}
<span class="hljs-keyword">public</span> Person <span class="hljs-title function_">buildPerson</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(
title,
firstName,
middleName,
lastName
);
}
}
}</pre></div>
<figure id="3bdd">
<div>
<div>
<img class="ratio" src="http://placehold.it/16x9">
<iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F0VxbgOKZ8%3Ffrom%3D26%26to%3D41&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2F0VxbgOKZ8%3Ffrom%3D26%26to%3D41&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
</div>
</div>
</figure></iframe></div></div></figure><p id="d6aa">Go back to <a href="https://readmedium.com/basic-class-syntax-c304635922ad">Basic Class Syntax</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/properties-introduction-fd98bc9c1056">Properties — Introduction</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&utm_medium=GabrielShanahan&utm_campaign=KotlinPrimer&utm_content=join-our-team&utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>