Constructors

Primary and secondary constructors, init blocks, defining properties as constructor arguments

Gabriel Shanahan
The Kotlin Primer

--

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

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

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

Tags: #KOTLIN FEATURE #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.

One of the differences between Java and Kotlin is how each language handles constructors.

In Java, constructors are methods with a special name. There can be any number of them, or none at all. They are all equivalent in terms of their properties — one is no more special than the other.

// A simple java class with a simple constructor
class MyIntegerJava {
Integer myInteger;

public MyIntegerJava(Integer…

--

--