<p>In this article, you will learn about constructors in Kotlin (both primary and secondary constructors) as well as initializer blocks with the help of examples.</p>
<h3><span style="color: #000080;"><strong>Constructor</strong></span></h3>
<ul>
<li>A constructor for a class is a special member function, mainly used to initialize the properties of the newly created object of that class type.</li>
<li>It is called implicitly, just after the memory is allocated for the object.</li>
<li>It is not mandatory for the programmer to write a constructor for a class.</li>
<li>When there is no constructor defined in the class by the programmer, the compiler implicitly provides a default constructor for the class.</li>
<li>In Kotlin, the default visibility of the constructor is <strong><span style="color: #008000;">public</span></strong>. However, the visibility can be changed to <span style="color: #008000;"><strong>private,</strong> <strong>protected or internal</strong></span>.</li>
</ul>
<h4><span style="color: #000080;"><strong>Types of Constructor</strong></span></h4>
<p>In Kotlin, there are two types of constructors:</p>
<ul>
<li><strong><span style="color: #0000ff;">Primary constructor</span></strong> &#8211; initializes the class.</li>
<li><span style="color: #0000ff;"><strong>Secondary constructor</strong></span> &#8211; initializes the class and allows you to put additional logic.</li>
</ul>
<h3><span style="color: #000080;"><strong>Primary Constructor</strong></span></h3>
<p>The <strong><span style="color: #008000;">primary</span> <span style="color: #008000;">constructor</span></strong> is part of the class header (contains the type parameters, the primary constructor, etc.), goes after the class name, using the <strong><span style="color: #0000ff;">constructor</span></strong> keyword.</p>
<p>In Kotlin, a class can have at most one primary constructor and the parameters are optional.</p>
<pre><strong><span style="color: #008000;">//Addition is the class_name</span></strong>
<strong><span style="color: #008000;">//constructor(val a: Int, val b: Int) is the class_header</span></strong>
class Addition <span style="color: #0000ff;"><strong>constructor</strong></span>(var a: Int, var b: Int) {
 <strong><span style="color: #008000;">//body of class</span></strong>
}</pre>
<p>The <strong><span style="color: #008000;">constructor</span></strong> keyword can be omitted if there is no annotation or visibility modifier specified.</p>
<pre><strong><span style="color: #008000;">//constructor keyword optional
//without annotation or access modifier</span></strong>
class Addition(val a: Int, val b: Int) {
 <strong><span style="color: #008000;">// body of class</span></strong>
}

<strong><span style="color: #008000;">//constructor keyword is needed
</span><span style="color: #008000;">//with</span></strong> <strong><span style="color: #008000;">annotation or access modifier</span></strong>
class Addition <span style="color: #0000ff;"><strong>private @Inject constructor</strong></span>(val a: Int, val b: Int) {
<strong><span style="color: #008000;"> // body of class</span></strong>
}</pre>
<h5><span style="color: #0000ff;"><strong>Example: Primary constructor</strong></span></h5>
<pre><strong><span style="color: #008000;">//main function</span></strong>
fun main(args: Array<;String>;) {
 var person = Person("Arun", 28)
 println("FirstName: ${person.pers_fname} \n Age: ${person.pers_age}")
}

<strong><span style="color: #008000;">//primary constructor
//constructor keyword is optional
</span></strong>class Person constructor(<span style="color: #008000;"><strong>val pers_fname: String, var pers_age: Int</strong></span>) {
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>FirstName: Arun
Age: 28</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>When the object of <span style="color: #0000ff;"><strong>Person</strong></span> class is created, values<span style="color: #008000;"> <strong>&#8220;Arun&#8221;</strong></span> and <span style="color: #008000;"><strong>28</strong></span> gets passed to the constructor.</li>
<li>The constructor parameters <span style="color: #0000ff;"><strong>pers_fname</strong></span> and <span style="color: #0000ff;"><strong>pers_</strong><strong>age</strong></span> get initialized with the values <strong>Arun</strong> and <strong>28</strong> respectively.</li>
<li>Inside <strong><span style="color: #008000;">main()</span></strong>, we can directly access the constructor parameters just like we access class properties.</li>
</ul>
<h4></h4>
<h4><strong><span style="color: #000080;">Primary Constructor with Initializer Block</span></strong></h4>
<p>The primary constructor cannot contain any code, the initialization code can be placed in a separate initializer block prefixed with the <strong><span style="color: #008000;">init</span></strong> keyword.</p>
<h5><span style="color: #0000ff;"><strong>Example </strong></span></h5>
<pre><strong><span style="color: #008000;">//main function</span></strong>
fun main(args: Array<;String>;) {
 var person = Person("Arun", 28)
}

<strong><span style="color: #008000;">//primary constructor</span></strong>
class Person <strong><span style="color: #0000ff;">constructor</span></strong>(pers_fname: String, pers_age: Int) {

 val firstName: String
 var age: Int

 <strong><span style="color: #008000;">// initializer block</span></strong>
 init {
 firstName = pers_fname
 age = pers_age

 println("FirstName: $firstName")
 println("Age: $age")
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>FirstName: Arun
Age: 28</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>When the object <span style="color: #008000;"><strong>person</strong></span> is created for the class <span style="color: #0000ff;"><strong>Person</strong></span>, the values <strong><span style="color: #008000;">&#8220;Arun&#8221;</span></strong> and <span style="color: #008000;"><strong>28</strong></span> gets passed to the parameter <span style="color: #0000ff;"><strong>pers_fname</strong></span> and <span style="color: #0000ff;"><strong>pers_age</strong></span> of the constructor.</li>
<li><span style="color: #0000ff;"><strong>firstName</strong></span> and <span style="color: #0000ff;"><strong>age </strong></span>are the two properties of <strong>Person</strong> class.</li>
<li><span style="color: #008000;"><strong>Initializer block</strong></span> is executed at the time of object creation, and not only initialize the properties but also prints to the standard output.</li>
</ul>
<h4></h4>
<h4><strong><span style="color: #000080;">Default values in </span></strong><strong><span style="color: #000080;">Primary Constructor </span></strong></h4>
<p>You need not pass all the parameters while declaring an object. You can initialize the constructor parameters with some default values.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h5><span style="color: #0000ff;"><strong>Example </strong></span></h5>
<pre><strong><span style="color: #008000;">//main function</span></strong>
fun main(args: Array<;String>;) {
 var person1 = Person("Ronald", 28)
<strong><span style="color: #008000;"> //default value for age will be used here</span></strong>
 var person2 = Person("Rohit")
<strong><span style="color: #008000;"> //default values for both parameters is used because no arguments passed</span> </strong>
 var person3 = Person()

}

<strong><span style="color: #008000;">//primary constructor</span></strong>
class Person constructor(pers_fname: String = "Arun", pers_age: Int = 26) {

 val firstName: String
 var age: Int

 <strong><span style="color: #008000;">// initializer block</span></strong>
 init {
 firstName = pers_fname
 age = pers_age

 println("FirstName: $firstName , Age: $age")
 
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>FirstName: Ronald , Age: 28
FirstName: Rohit , Age: 26
FirstName: Arun , Age: 26</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>Here, we have initialized the constructor parameters with some default values <span style="color: #008000;"><strong>pers_</strong><strong><span style="color: #008000;">fname = </span>&#8220;Arun&#8221; and pers_age = 26</strong></span>.</li>
<li>So, whenever we skip the parameter values while creating objects, the compiler will automatically initialize the object properties with the default values.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Secondary Constructor</strong></span></h3>
<p><span style="color: var(--color-text);"><strong><span style="color: #008000;">Secondary constructor</span></strong> initializes the class (properties) and allows you to put additional logic to the class as well.</span></p>
<p><span style="color: var(--color-text);"> They are prefixed with the </span><span style="color: #008000;"><strong>constructor</strong></span><span style="color: var(--color-text);"> keyword and</span> are written inside the body of class.</p>
<p>In Kotlin, a class may have one or more secondary constructors and which secondary constructor will be called is decided by the compiler based on the arguments received.</p>
<h5><span style="color: #0000ff;"><strong>Example: Secondary constructor</strong></span></h5>
<pre>fun main(args: Array<;String>;) {
 Addition(2, 3)
 Addition(2, 3, 4)
 Addition(2, 3, 4, 5)
}

<strong><span style="color: #008000;">//class with three secondary constructors</span></strong>
class Addition {
<strong><span style="color: #008000;">//secondary constructor with two arguments</span></strong>
 constructor(a: Int, b: Int) {
 var c = a + b
 println("Sum of 2, 3 : ${c}")
 }
<strong><span style="color: #008000;">//secondary constructor with three arguments</span></strong>
 constructor(a: Int, b: Int, c: Int) {
 var d = a + b + c
 println("Sum of 2, 3, 4 : ${d}")
 }
<strong><span style="color: #008000;">//secondary constructor with four arguments</span></strong>
 constructor(a: Int, b: Int, c: Int, d: Int) {
 var e = a + b + c + d
 println("Sum of 2, 3, 4, 5 : ${e}")
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Sum of 2, 3 : 5
Sum of 2, 3, 4 : 9
Sum of 2, 3, 4, 5 : 14</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>Firstly, the secondary constructor (with two arguments) is called, which prints the sum of <strong>two</strong> numbers.</li>
<li>Then, the secondary constructor (with three arguments) is called, which prints the sum of <strong>three</strong> numbers.</li>
<li>And then, the secondary constructor (with four arguments) is called, which prints the sum of <strong>four</strong> numbers.</li>
</ul>
<h4><span style="color: #000080;"><strong>Calling one Secondary constructor from another</strong></span></h4>
<p>A secondary constructor may call another secondary constructor of the same class using <span style="color: #008000;"><strong>this </strong><span style="color: #000000;">keyword</span></span>.</p>
<h5><span style="color: #0000ff;"><strong>Example </strong></span></h5>
<pre>fun main(args: Array<;String>;) {
 Addition(2, 3)
}

class Addition {
 constructor(a: Int, b: Int) : this(a, b, 4) {
 var sumOfTwo = a + b
 println("The Sum of two numbers 2, 3 is: ${sumOfTwo}")
 }

 constructor(a: Int, b: Int, c: Int) {
 var sumOfThree = a + b + c
 println("The Sum of three numbers 2, 3, 4 is: ${sumOfThree}")
 }

}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>The Sum of three numbers 2, 3, 4 is: 9
The Sum of two numbers 2, 3 is: 5</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>Inside <span style="color: #0000ff;"><strong>Addition</strong></span> class, the secondary constructor (with two arguments) is calling the secondary constructor (with three arguments) using <span style="color: #008000;"><strong>this</strong></span> keyword.</li>
<li>So, the compiler will first execute secondary constructor (with three arguments) then secondary constructor (with two arguments).</li>
</ul>
<h4></h4>
<h4><span style="color: #000080;"><strong>Calling parent class secondary constructor from child class secondary constructor </strong></span></h4>
<p>We can call the secondary constructor of the parent class from the child class using the <strong><span style="color: #008000;">super</span></strong> keyword.</p>
<h5><span style="color: #0000ff;"><strong>Example </strong></span></h5>
<pre>fun main(args: Array<;String>;) {
 Child("Arun", "Verma")
}

open class Parent {
 constructor (pers_fname: String, pers_lname: String, pers_age: Int) {
 var firstName: String = pers_fname
 var lastName: String = pers_lname
 var age: Int = pers_age
 print("FirstName : $firstName, ")
 print("LastName: $lastName, ")
 println("Age: $age")

 }
}

class Child : Parent {
 constructor (pers_fname: String, pers_lname: String) : <span style="color: #008000;"><strong>super(pers_fname, pers_lname, 26)</strong></span> {
 var firstName: String = pers_fname
 var lastName: String = pers_lname
 print("FirstName: $firstName, ")
 println("LastName: $lastName")
 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>FirstName : Arun, LastName: Verma, Age: 26
FirstName: Arun, LastName: Verma</pre>
<h5><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>
<ul>
<li>Inside <strong><span style="color: #0000ff;">Child</span></strong> class, secondary constructor (with two arguments) is calling <span style="color: #0000ff;"><strong>Parent</strong></span> class secondary constructor (with three arguments) using <span style="color: #008000;"><strong>super</strong></span> keyword.</li>
<li>So, the compiler will first execute Parent class secondary constructor then Child class secondary constructor.</li>
</ul>
<p> ;</p>


