One of the great things about Kotlin is its brevity and as part of that brevity, Kotlin works hard to eliminate what we often refer to as boilerplate code .. blocks of code that we frequently write with little or no change.

As great as Kotlin’s brevity is (and I do love it), it can often make understanding Kotlin challenging when first learning the language. Consider the following Kotlin class declaration:

class PersonKotlin(val firstName:String, val lastName:String, var age:Int)

That’s a complete declaration of a class named PersonKotlin that includes a constructor and 3 properties.

A look at some boilerplate code

To give us some context, let’s look at what is essentially the same class declared in Java.

public class PersonJava {
    private String firstName;
    private String lastName;
    private int age;
    
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public PersonJava(String firstNameIn, String lastNameIn, int ageIn) {
        firstName = firstNameIn;
        lastName = lastNameIn;
        age = ageIn;
    }
}

In the Java version of the class, things become a bit more clear. We basically want the class to have 2 read-only properties (firstName, and lastName) and one read-write property (age).

As we look at the Java version of the class, we can see just how much boilerplate code is required. We have to declare private fields to store the firstName, lastName, and age; we create getter methods for the 2 read-only properties along with both a getter and setter for the read-write property. Then we have our constructor whose only real job is to accept initial values and then assign those values to the fields that correspond to our properties. That’s a lot of boilerplate code (property getters, property setters, and assignments to give properties their intial values).

A closer look at the Kotlin class declaration

To help us understand what’s happening in the Kotlin class, let’s look at a slightly more expressive way we can write the Kotlin class.

class PersonKotlin(firstNameIn:String, lastNameIn:String, ageIn: Int) {
    val firstName = firstNameIn
    val lastName = lastNameIn
    var age = ageIn
}

With this we can see our 3 properties a bit more clearly. By declaring firstName and lastName with “val” we’re indicating that they’re read-only properties (can’t be changed once initially set). Using “var” to declare age indicates that it’s a read-write property. So the “val” and “var” keywords allow us to create properties and indicate whether they’re read-only or read-write without having to write boilerplate getter/setter code as we did in Java.

The 3 values in the parenthesis after the class name are the class’ constructor parameters. As is often the case, the only purpose of those parameters is to receive the values that we’ll use to initialize the class properties.

Back to brevity

Since the only purpose of the constructor parameters is to initialize the properties, Kotlin allows us declare the properties themselves right there in the constructor parameter list. This allows us to get rid of the boilerplate code required to explicitly assign the constructor parameter values to the properties.

So with that in mind, we can go back our original Kotlin class declaration.

class PersonKotlin(val firstName:String, val lastName:String, var age:Int)

With this, by including the val and var keywords in the parameter list we’re able to both declare the properties and have their initial values assigned as part of class construction.

So we can use our class like this…

val person = PersonKotlin("Maria", "Jones", 27)

// Set greeting to "Hello Maria Jones
val greeting = "Hello " + person.firstName + " " + person.lastName

// Happy birthday
person.age += 1;

We declare a local variable named person and create a new instance of PersonKotlin passing initial values for the firstName, lastName, and age properties. We then use the firstName and lastName properties to create a string that says “Hello Maria Jones”. Finally, we celebrate Maria’s birthday by increasing the age property by one.

… and we get all of this from a Kotlin class that we can literally declare in a single line.

Posted by hedgehogjim

30+ years as a professional software dev, 17+ years developing mobile solutions, Pluralsight Author, Founder JWHH, LLC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s