Secondary Designer Kotlin

How to declare a secondary constructor in Kotlin?

Is there any documentation about this?

The following does not compile ...

class C(a : Int) { // Secondary constructor this(s : String) : this(s.length) { ... } } 
+79
syntax constructor kotlin
Oct 10 '13 at 15:07
source share
10 answers

Update : Since M11 (0.11. *), Kotlin supports secondary constructors .




Kotlin now only supports primary constructors (secondary constructors may be supported later).

Most use cases for secondary designers are solved in one of the following ways:

Technique 1. (solves your case) Define a factory method next to your class

 fun C(s: String) = C(s.length) class C(a: Int) { ... } 

using:

 val c1 = C(1) // constructor val c2 = C("str") // factory method 

Technique 2. (may also be useful) Define default values ​​for parameters

 class C(name: String? = null) {...} 

using:

 val c1 = C("foo") // parameter passed explicitly val c2 = C() // default value used 

Note that the default values ​​work for any function, not just for constructors.

Technique 3. (when you need encapsulation) Use the factory method defined in the companion object

Sometimes you need your private constructor and only the factory method available to clients. So far, this is only possible with the factory method defined in the companion object :

 class C private (s: Int) { companion object { fun new(s: String) = C(s.length) } } 

using:

 val c = C.new("foo") 
+76
Oct 11 '13 at 5:18
source share

As documentation items, you can use an auxiliary constructor in this way

 class GoogleMapsRestApiClient constructor(val baseUrl: String) { constructor() : this("https://api.whatever.com/") } 

Remember that you must extend the behavior of the first constructor.

+22
Apr 24 '16 at 18:01
source share

to declare a Kotlin secondary constructor, simply use the constructor keyword: for example

this is the primary constructor:

 class Person constructor(firstName: String) { } 

or

 class Person(firstName: String) { } 

for auxiliary constructor code:

 class Person(val name: String) { constructor(name: String, parent: Person) : this(name) { parent.children.add(this) } } 

need to call the primary constructor otherwise the compiler will cause the following error

 Primary constructor call expected 
+14
Jul 27 '17 at 2:31 on
source share

Constructors with init :

 class PhoneWatcher : TextWatcher { private val editText: EditText private val mask: String private var variable1: Boolean = false private var variable2: Boolean = false init { variable1 = false variable2 = false } constructor(editText: EditText) : this(editText, "##-###-###-####") constructor(editText: EditText, mask: String) { this.editText = editText this.mask = mask } ... } 
+7
Oct. 16 '18 at 15:57
source share

You can define several constructors in Kotlin using the constructor but you need to skip the class AuthLog(_data: String) of the default constructor class AuthLog(_data: String)

 class AuthLog { constructor(_data: String): this(_data, -1) constructor(_numberOfData: Int): this("From count ", _numberOfData) private constructor(_data: String, _numberOfData: Int) } 

See here for more details.

Refresh

Now you can define a default constructor

 class AuthLog(_data: String, _numberOfData: Int) { constructor(_data: String): this(_data, -1) { //TODO: Add some code here if you want } constructor(_numberOfData: Int): this("From count", _numberOfData) } 
+5
Apr 04 '18 at 18:54
source share

I just saw this question, and I think that there may be another technique that sounds even better than the ones that Andrey suggested.

 class C(a: Int) { class object { fun invoke(name: String) = C(name.length) } } 

So you can write something like val c:C = C(3) or val c:C = C("abc") , because invoke methods work just like apply methods work in Scala.

Update

Secondary constructors are currently part of the language specification, so this workaround should not be used.

+4
Nov 18 '14 at 20:58
source share
 class Person(val name: String) { constructor(name: String, parent: Person) : this(name) { parent.children.add(this) } } 

you can try this.

+1
Jun 11 '17 at 16:26
source share

The below code snippet should work

 class C(a:Int){ constructor(s:String):this(s.length){..} } 
+1
Jul 24. '17 at 0:39
source share

kotlin Example helper constructor

 class Person(name: String){ var name="" var age=0 constructor(age :Int,name : String) : this(name){ this.age=age this.name=name } fun display(){ print("Kotlin Secondary constructor $name , $age") } } 

Main function

 fun main(args : Array<String>){ var objd=Person(25,"Deven") objd.display() } 
0
Aug 14 '17 at 12:00
source share

I was a little confused by most of the answers. To make it easier to understand, I am adding an example with a lot of elements:

  @JsonInclude(JsonInclude.Include.NON_NULL) data class Response(val code: String) { var description: String? = null var value: String? = null constructor(code: String, description: String?) : this(code) { this.description = description } constructor(code: String, description: String?, value: String) : this(code, description) { this.value = value } } 
0
Apr 6 '19 at 17:14
source share



All Articles