Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructors in Kotlin

I am learning Kotlin from official docs, I created one class like below where I created one constructor which has two parameters. Body of constructor is in init block.

class Person(name: String, surname: String) {     init {         Log.d("App", "Hello");     } } 

Well, I want to create one more constructor which will take one parameter in a constructor. What is the way to do in Kotlin

like image 405
N Sharma Avatar asked May 31 '17 05:05

N Sharma


People also ask

How do Kotlin constructors work?

Constructors A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is a part of the class header, and it goes after the class name and optional type parameters. The primary constructor cannot contain any code.

Can Kotlin object have constructor?

In Kotlin, a class can have a primary constructor and one or more additional secondary constructors.


2 Answers

Well init is not body of constructor. It is called after primary constructor with the context of primary constructor.

As given in Official documentation:

The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword:

class Customer(name: String) {     init {         logger.info("Customer initialized with value ${name}")     } } 

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

class Customer(name: String) {     val customerKey = name.toUpperCase() } 

In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax:

class Person(val firstName: String, val lastName: String, var age: Int) {     // ... } 

As per your question you can add a constructor to accept one parameter like following:

class Person(name: String, surname: String) {      constructor(name: String) : this(name, "") {         // constructor body     }      init {         Log.d("App", "Hello");     } } 

But it doesn't look right as we are unnecessary passing second argument empty string. So we can order constructor like following:

class Person(name: String) {      constructor(name: String, surname: String) : this(name) {         // constructor body     }      init {         Log.d("App", "Hello");     } } 

Hope it helps.

like image 111
chandil03 Avatar answered Sep 18 '22 06:09

chandil03


First way with empty values

// (name: String, surname: String)  default constructor signature class Person(name: String, surname: String) {      // init block , represents the body of default constructor      init {         Log.d("primary", "Hello");     }      // secondary constructor      // this(name,"") call to default constructor     constructor(name : String):this(name,""){         Log.d("secondary", "Hello");     } } 

why this(name,"")

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

or

kotlin won't allow to use null like this(name,null) so use ? to represent null values with type, surname: String?

class Person(name: String, surname: String?) {      init {         Log.d("primary", "Hello");     }      constructor(name : String):this(name,null){         Log.d("secondary", "Hello");     } } 
like image 41
Pavneet_Singh Avatar answered Sep 19 '22 06:09

Pavneet_Singh