Kotlin Constructors Tutorial

In this section, we will learn what the constructors are and how to use them in Kotlin.

Note: we’re assuming you’re already familiar with the Kotlin classes and objects.

What is the constructor in Kotlin?

The Constructor in Kotlin is a function that will be invoked automatically when we create an object from a class.

We use a constructor to assign values to properties of the objects right when the object is being created.

By default, each class has a constructor implicitly set for it. This constructor does not have any parameter though!

Note: to be clear, when creating an object from a class, we put a pair of parentheses after the name of a class, right? That is the constructor that we’re calling! Now, because this constructor does not have a parameter, we didn’t set any argument for it when calling it using the name of a class.

Alright, now let’s get into the details on how we can create constructors in Kotlin.

Note: in order to call a constructor, we use the name of the class!

How to Create a Constructor in Kotlin: Constructor Syntax

There are two ways to create a constructor for a class.

This is the syntax of the first way of creating a constructor in a class:

class ClassName (parameter_name: data-type, parameter_name2:data-type, parameter_name_n:data-type){

#body…

}

This syntax is exactly like the way we create the parameters of a function! Basically, we put a pair of parentheses after the name of a class and then inside it define the parameters we want to be filled at the time of object creation!

For example, if we set like 3 parameters, then when creating an object from the class, we need to pass three arguments to the constructor (the pair of parentheses that comes after the class at the time of object creation) and those arguments will be assigned to each of the parameters of the constructor.

Now we can declare one or more properties inside the body of the class and assign the values of these parameters to the properties.

The second way of creating a constructor for a class is by using either val or var keywords for the parameters of the constructor!

For example:

class ClassName (val param1:data-type, var param2:data-type, val param_n:data-type){…}

Using this syntax, the parameters of the constructor automatically become the properties of the class! Basically, now there’s no need to create a new property in the class if these parameters server the purpose for the objects that are going to be created using the class. (We will talk more about this syntax later in this section)

Example: creating a constructor in Kotlin

class Employee (fName:String, lName:String, ag:Int){
    var firstName:String = fName
    var lastName = lName 
    var age = ag  
    
}

fun main(){
    var emp = Employee("John","Doe",21)

    println(emp.firstName)
    println(emp.lastName)
    println(emp.age)

}

Output:

John

Doe

21

In this example, the constructor of the Employee class contains three parameters. The first two parameters are of type String and the last one is of type Int. This means when creating an object from this class, we need to pass three arguments which the first two ones are of type String and they will be passed to the fName and lName parameters and the last argument must be of type Int which will be assigned to the ag parameter.

We then used these parameters and assigned them to the properties of the class firstName , lastName and the age properties, respectively.

Now our properties are initialized, and we can use them right away after creating the object.

Kotlin Constructors and var and val keywords:

In the last example, you saw that we first created the parameters of the constructor; after that we’ve created the properties of the class and assigned the values of the parameters to the properties.

But Kotlin has shorten this process by allowing developers to define the properties of the class right inside the parameters of the constructors!

Basically, by adding the val or var keyword in front of the names of parameters, they become the properties of the class automatically.

Note: if you want to have a constant property, then use the val keyword for the target parameter, but if you want to have a variable property, then set the var keyword in front of the target parameter.

Example: creating Kotlin constructor with val and var keywords

class Employee (var firstName:String, var lastName:String, var age:Int){
}

fun main(){
    var emp = Employee("John","Doe",21)

    println(emp.firstName)
    println(emp.lastName)
    println(emp.age)

}

Output:

John

Doe

21

In this example, the firstName, lastName, and age parameters of the constructor are declared using the var keyword and so they become the properties of the newly created object as well.

That’s how we could use the emp object and call the mentioned properties.

Kotlin Default Constructor

As mentioned before, a class by default has a constructor! This constructor is added behind the scene and it doesn’t have any parameter!

For example, let’s say you’ve created a class like:

class Car{

#instructions...

}

Behind the scene Kotlin compiler will add the empty constructor as:

class Car(){

#instructions…

}

That’s how we could call a class in order to create an object from it without passing any arguments to it! (Because the default constructor does not have any parameter!)

Note: when creating a constructor for a class, you no-longer can use the default constructor anymore! This means you should always use the custom constructor that you’ve created for the class and pass a related number of arguments at the time of object creation!

Example: creating object using default constructor of a class

class Employee (var firstName:String, var lastName:String, var age:Int){
}

fun main(){
    var emp = Employee()

    println(emp.firstName)
    println(emp.lastName)
    println(emp.age)

}

Output:

error: no value passed for parameter 'firstName'

As you can see, for the Employee class, we have explicitly defined a constructor! This means we can’t use the default constructor of the class anymore. If we do so, we will get an error, like the one we got in this example.

More to Read:

Kotlin Constructor Overloading (AKA Secondary constructors)

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies