https://kotlinlang.org logo
Title
k

Ken McDonald

06/06/2019, 2:18 PM
Just getting started in Kotlin and went through the free Udacity course...coming from a C++ background. Question: I understand there are not really the same kind of class constructors in Kotlin like C++ with overloads for different constructors and such, but is the init() function is kind of the same thing? We can have any number of init() inside a Kotlin class; they all run and are executed sequentially as encountered in the class description? There's no way to do conditional init() execution when defining them, only by crafting decision logic inside them?
s

Shawn

06/06/2019, 2:20 PM
If I understand what you’re asking, you might actually be looking for secondary constructors https://kotlinlang.org/docs/reference/classes.html#constructors
they don’t work entirely like overloaded constructors in C++ or Java but I think they line up with what you mentioned about conditional execution a bit better than
init
blocks
also, to be clear, init blocks don’t define a function/method, they’re just blocks run at initialization time
k

Ken McDonald

06/06/2019, 2:39 PM
Yeah regarding init(), I could see they were for class initialization when instantiated and great for that. I just wanted to better understand the corollary to C++ style. Thanks for the other link. Seems the Kotlin implementation of this secondary idea creates a dependency on the primary constructor. Guess I'll need more practice to understand what "If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor."
s

Shawn

06/06/2019, 3:31 PM
When declaring a secondary constructor for a class which has a primary constructor that takes arguments, the secondary is required call the primary constructor. This is enforced in a rather strict way by forcing folks to place the call to
this()
in the constructor declaration, before the actual body. Here’s a very contrived example lol:
class Foo(val a: Int) {
    constructor(notA: String) : this(notA.length)
}
i

igor.wojda

06/06/2019, 3:31 PM
You can think about
init
as body of primary constructor. BTW If the class has no primary constructor, then each secondary constructor has to initialise the base type using the
super
keyword, or to delegate to another constructor which does that.
open class Foo(val a: Int) {

}

class Bar : Foo {
    constructor(a: Int) : super(a) {
        
    }

    constructor(a: Int, b: Int) : super(a) {
        
    }
}
k

Ken McDonald

06/06/2019, 4:07 PM
Thanks for those examples. Very enlightening. @Shawn I suppose my take-away from this is that in Kotlin, secondary constructors are not discrete like in C++ and must relate to the primary in some way, most notably with required parameters in the primary constructor. This is not the case in C++ if my thinking is correct. @igor.wojda That article is really helpful. Yeah even in Kotlin, inheritance and all those layers of implicit declarations and initialization orders can be tricky to discern.