avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3701

Abstract

ue, and <i>not</i> properties (a pair of functions).</p><h1 id="2298">Exercises</h1><p id="b562">Now that we know how properties behave, we are ready to write a mutable version of <code>Person</code> from a previous lesson.</p><p id="c615">Rewrite this class in Kotlin:</p><div id="dcb1"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Person</span> { <span class="hljs-keyword">private</span> String title; <span class="hljs-keyword">private</span> String firstName; <span class="hljs-keyword">private</span> String middleName; <span class="hljs-keyword">private</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> String <span class="hljs-title function_">getTitle</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> title;
}

<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> String <span class="hljs-title function_">getFirstName</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> firstName;
}

<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> String <span class="hljs-title function_">getMiddleName</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> middleName;
}

<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> String <span class="hljs-title function_">getLastName</span><span class="hljs-params">()</span> {
    <span class="hljs-keyword">return</span> lastName;
}

<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> <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">S

Options

tring</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="91e4"> <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%2FZRyxvfPEz%3Ffrom%3D38%26to%3D42&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FZRyxvfPEz%3Ffrom%3D38%26to%3D42&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="53df">Go back to <a href="https://readmedium.com/backing-fields-e0efd8ef6895">Backing Fields</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/compile-time-constants-38dc1d2443b7">Compile Time Constants</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&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Accessors, continued

Custom getters vs. direct property initialization and defining properties outside classes

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI++ #EXERCISE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Let’s emphasize the difference between defining a custom getter vs. initializing a property directly:

In the above, also note how Kotlin automatically translates Java properties into Kotlin properties. The actual method defined on the Instant class is public long getEpochSecond(). This is done with all Java code called from Kotlin.

While fields and properties are almost exclusively discussed in the context of classes, the definitions at the top are not bound to the concept of a class. Indeed, Kotlin also permits properties defined at the top-level, outside of classes:

However, it is not possible to define properties with custom getters/setters inside functions. When used inside functions, val and var declare variables, which contain some value, and not properties (a pair of functions).

Exercises

Now that we know how properties behave, we are ready to write a mutable version of Person from a previous lesson.

Rewrite this class in Kotlin:

public class Person {
    private String title;
    private String firstName;
    private String middleName;
    private String lastName;

    public Person(
            String title, 
            String firstName, 
            String middleName, 
            String lastName
    ) {
        this.title = title;
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }

    public Person(String firstName, String lastName) {
        this("", firstName, "", lastName);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(final String newTitle) {
        title = newTitle;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String newFirstName) {
        firstName = newFirstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(final String newMiddleName) {
        middleName = newMiddleName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String newLastName) {
        lastName = newLastName;
    }

    public static class Builder {
        private String title = "";
        private String firstName = "";
        private String middleName = "";
        private String lastName = "";

        public void setTitle(final String newTitle) {
            title = newTitle;
        }

        public void setFirstName(final String newFirstName) {
            firstName = newFirstName;
        }

        public void setMiddleName(final String newMiddleName) {
            middleName = newMiddleName;
        }

        public void setLastName(final String newLastName) {
            lastName = newLastName;
        }

        public Person buildPerson() {
            return new Person(
                    title, 
                    firstName,
                    middleName,
                    lastName
            );
        }
    }
}

Go back to Backing Fields, jump to the Table of Contents, or continue to Compile Time Constants.

Join me in Etnetera
Kotlin
Java
Programming
Getters And Setters
Properties
Recommended from ReadMedium