Kotlin - Constructors

In this chapter we can break down the basics of constructors in Kotlin

Kotlin has sorts of constructor: number one constructor and secondary constructor

Kotlin the main constructor instates the elegance


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

/class body 

In the above precedent, we've proclaimed the main constructor inside the enclosure 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

fun important(args: Array) 

val person1 = Person("welookups.Com", 18) 

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 = 18 


Other precedent auxiliary constructor and are utilizing the above case to uphold the indistinguishable. 


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.ComWelcome to the case of Secondary constructor, Your Age is-18