Kotlin Constructors Tutorial 🎯

beginner
17 min

Kotlin Constructors Tutorial 🎯

Welcome to the Kotlin Constructors Tutorial! In this lesson, we'll delve into one of the most fundamental concepts in Kotlin – Constructors. By the end of this tutorial, you'll understand why constructors are essential and how to create them for your classes.

What is a Constructor in Kotlin? 📝

A constructor in Kotlin is a special method that is used to initialize objects of a class. The main purpose of a constructor is to set initial values for the properties of an object.

kotlin
class Person(name: String, age: Int) { // Properties var myName: String = name var myAge: Int = age }

In the example above, we have defined a class Person with two properties - name and age. The class also has a constructor that takes name and age as parameters. When we create an instance of this class, the constructor is automatically called, and the properties are initialized with the provided values.

Primary Constructor 💡

Every class in Kotlin has a primary constructor, which is created automatically by the compiler if you don't create one yourself. The primary constructor is used to initialize the properties of a class.

kotlin
class Person(name: String, age: Int) { // Properties var myName: String = name var myAge: Int = age } // Creating an instance of Person val person = Person("John Doe", 30)

In the example above, we have not defined a constructor explicitly, but we can still create an instance of the Person class. This is because the compiler has created a primary constructor for us.

Secondary Constructors 💡

You can create multiple constructors in a class to provide different ways of initializing objects. These additional constructors are called secondary constructors.

kotlin
class Person(name: String, age: Int) { // Properties var myName: String = name var myAge: Int = age // Secondary constructor constructor(name: String) : this(name, 0) { println("Secondary constructor called!") } } // Creating instances using primary and secondary constructors val person1 = Person("John Doe", 30) val person2 = Person("Jane Doe")

In the example above, we have defined a secondary constructor that calls the primary constructor. When we create an instance using the secondary constructor, the message "Secondary constructor called!" will be printed to the console.

Initializer Blocks 💡

Initializer blocks are pieces of code that are executed when an object is created. They are useful for initializing properties or performing some setup before the constructor is called.

kotlin
class Person(name: String, age: Int) { // Properties var myName: String = name var myAge: Int = age // Initializer block init { println("Initializer block called!") checkValidName(name) } // Function to check if the name is valid fun checkValidName(name: String) { if (name.length < 3) { throw IllegalArgumentException("Name must be at least 3 characters long!") } } } // Creating an instance of Person val person = Person("John", 30)

In the example above, we have defined an initializer block that prints a message and checks if the name is valid. If the name is not valid, an IllegalArgumentException is thrown.

Quiz 📝

Quick Quiz
Question 1 of 1

What is the purpose of a constructor in Kotlin?

Quick Quiz
Question 1 of 1

Is it mandatory to define a constructor in a Kotlin class?