A constructor plays a very important role in the initialization of class objects. It is a special member function that has no return type. In this tutorial on the Kotlin constructors, you will explore the Kotlin constructor in detail with the help of examples.  

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Is a Kotlin Constructor?

A constructor can be defined as a type of member function or a special function that is used to initialize the properties of the class. When the class object is created, it automatically calls the constructors before any function call.

For example, whenever you place an order at a restaurant, it is a general protocol that they will provide you with the cutlery and dishes before the meals are offered. So this is the protocol that is done automatically whenever a customer orders something. Similarly, whenever you create an object of the class, the constructor is called automatically before it makes the function calls. 

The working of the constructor in Kotlin is a bit different, which you will look into later.

Now, understand the need for a constructor and the types of constructors.

What Is the Need of the Constructor?

The class has many properties, which must be initialized. To initialize those properties, you need an object, and the Constructor is used to initialize the object with a particular initial value. 

Now, have a look at types of constructors.

There are two types of Constructors in Kotlin.

  • Primary Constructor

  • Secondary Constructor

Primary Constructor

In Kotlin language, the Constructor is declared with the class header, unlike Java, where you have to declare the Constructor in the class body.

Now, have a look at an example.

/Kotlin_Constructors_1

Here, (val name: String, var roll: Int), is the primary Constructor. This primary constructor has two properties: name and roll.

Kotlin_Constructors_2.

Here, S_record is the object of the Record class, and when it is created, it initializes the name and roll with values Ram and 7, respectively.

The output of the above function is 

Kotlin_Constructors_3.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Primary Constructor With Initializer Block

The primary constructor in Kotlin cannot contain any code, but you have to put the initialization logic code somewhere. So you use the init block to put the initialization code. The syntax of this block contains the init keyword and the code block.

Kotlin_Constructors_4.

As you can see here, name and roll are accepting the values Ram and 7. Both name and roll are used without var or val, so they are not the property of the class.

The record class has two properties, S_name and S_roll; when the object S_record is created, the init block executes, and it will initialize and print the properties. 

Now it’s time to move on to the next topic of Kotlin constructors.

Default Values in Constructor

You can also put default values in the constructor.

Kotlin_Constructors_5.

Here, in the parameters, Somebody and 0 are the default values of the constructor. If either the name or roll of a particular student is not found, then only these default values will be printed.

Kotlin_Constructors_6

In the above output, you can see that for Student 1, i.e. Ram, you have the name and roll number; that is why both name and roll are printed and not the default values.

For Student 2, you have the name Arjun but not the roll number, so that is why the default value is printed in place of the roll number.

For Student 3, you don't have both the name and roll number, so in this case, default values are printed in both places. 

Now, understand the Secondary constructor.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Secondary Constructor

The secondary constructor is created using the keyword constructor. It is used to initialize a group of values. In Kotlin, a single class can have more than one secondary constructor.

Now, have a look at an example of a secondary constructor.

Kotlin_Constructors_7

Here, you can see the object of the class is created, and the values are getting assigned in the secondary constructor.

Below is the output of the above example.

Kotlin_Constructors_8

You can also use primary and secondary constructor in the same class, but for that, you have to authorize the secondary constructor to the primary constructor, which is done with the help of this() keyword.

Kotlin_Constructors_9.

You can also call one secondary constructor from another secondary constructor. Now, have a look at an example.

Kotlin_Constructors_10.

As you can see, there are two secondary constructors, one having a single parameter name and the other having two parameters - name and age. With the help of this() keyword, the secondary Constructor is getting called.

Below is the output of the above example.

Kotlin_Constructors_11.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

In this tutorial on Kotlin constructor, you looked at and understood the need of Kotlin constructor and learn what Kotlin constructor is. You also learned about types of constructors, i.e. Primary and Secondary constructors. You also understand the default values in Constructor and Primary Constructor with the initializer block.

If you are looking to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.

Do you have any questions regarding this tutorial on Kotlin Constructor? If you do, then you can put them in the comments section. We’ll help you solve your queries. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449