Kotlin - Constructors

In this chapter we will show you about constructors in Kotlin.

Kotlin has sorts of constructor:one is the primary constructor and the other is the secondary constructor.

One Kotlin class can have one primary constructor, and one or more secondary constructor. The primary constructor can be declared at class header level as shown in the following example.
class Person(val firstName: String, var age: Int) {
   // class body
In the above example, we've declared the main constructor inside the parenthesis. first name is think about least complex as it's far announced as "val", in the meantime as the field age can be altered. In the accompanying example, we can utilize the main constructor

where we have created a secondary constructor and are using the above example to implement the same.

fun main(args: Array<String>) {
   val person1 = Person("welookups.com", 15)
   println("First Name = ${person1.firstName}")
   println("Age = ${person1.age}")
class Person(val firstName: String, var age: Int) {
The above bit of code will mechanically instate the two factors and offer the consequent yield inside the program.
First Name = welookups.com
Age = 15
Kotlin allows to create one or more secondary constructors for your class. This secondary constructor is created using the “constructor” keyword.

fun principal(args: Array) 

val HUman = HUman("welookups.Com", 18) 


"Welcome to the occurrence of Secondary constructor, Your Age is-$HUman.Age") 

class HUman(val firstName: String, var age: Int) 

val message:String = "Oh!!!" 

constructor(name : String , age :Int ,message :String):this(name,age) 




Gracious!!! Welookups.Com Welcome to the case of Secondary constructor, Your Age is-18