Kotlin
Java

Constructors
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-09-18

Let's continue our discussion of Java objects. Remember that bit of syntax that looked like a method call when we create a new Kotlin object? Well, it was! Today we'll talk about what it does.

Constructors

Each time an instance of a Kotlin class is created, code is run as part of a special function called a constructor. The best way to see this is to examine a different syntax for class declaration:

Discuss the empty constructor form of Kotlin class declaration. Point out that instance variables can be declared in the class body, but must be initialized so that they have values after the class is created. Also point out that, in this case, the instance is created using an empty constructor.

Beginning with the empty constructor class syntax shown above, we can now explicitly declare a constructor method using the following syntax:

However, this pattern of using the constructor to set initial property values is so common that Kotlin provides a shortcut—which we've already been using! Let's see how:

Discuss the primary constructor syntax and show how it is much more compact than the explicit constructor version.

Two Ways to Declare Properties

As shown above, Kotlin provides us with two different ways to declare properties on our classes. First, we can add them to the primary constructor prefaced with val or var:

In this case we must provide the name (String) and age (Double) every time we create a Person.

Alternatively, we can declare the fields inside the class declaration and omit the primary constructor:

In this case we don't provide the name or age when we create a Person, but each Person still has a name and age property. In contrast to the example above we also must provide default values for each field, since Kotlin needs to know how to set them since they are not set when the instance is created.

Finally, in this case it usually makes less sense to declare the fields as immutable using val, since to store data in them we will need to change them after the instance is created.

We can also mix these two approaches:

init Blocks and Secondary Constructors

When if we want to make sure that a person's age is always positive? We'd like to continue to use the primary constructor syntax, since it is compact and elegant:

However, we also need to run some additional code when the Person is created to make sure that the age is positive. To do this we can use an initializer block:

init blocks are run every time any constructor is called, and in the order in which they appear in the class. You can put any code you want in them, and they can be used to set properties during creation and perform other tasks.

Finally, sometimes we want more than one way to create an instance of a class. For our Person class, perhaps we want to make the age optional and have it be zero by default.

Kotlin provides two ways of doing this. First, we can use both a primary constructor and one or more secondary constructors:

Provide an overview of the secondary constructor syntax, including the call to the primary constructor using this.

Note that we now have two constructors: one that takes two arguments (a name and an age) and a second that only takes a name. This is due to the secondary constructor declared as constructor(name: String) : this(name, 0.0). Secondary constructors are declared using the constructor syntax we saw above. But, when a primary constructor exists a secondary constructor must call it using this and pass all of the required fields, as shown above.

However, a better alternative is usually to use default parameter values for the primary constructor:

We have not discussed default parameter values for Kotlin methods yet, but we will soon. This approach works for any Kotlin method, including constructors, and allows us to provide default values for any and all required parameters to a method or function. We'll return to this, but for now simply observe that it eliminates the need for the secondary constructor.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough