I have a library written in Kotlin which is then u...
# getting-started
j
I have a library written in Kotlin which is then used as a dependency from Java code. Is it expected that KClass#primaryConstructor returns null?
Copy code
clazz.primaryConstructor!!.call(param)
When this line is executed from Kotlin code the primary consructor is returned. In Java I get a null value.
w
I don't know if I understand your question, but for example, the following will cause the
primaryConstructor
to be
null
even in a
Kotlin
class.
Copy code
import kotlin.reflect.full.primaryConstructor

class C {
    val v: Int

    constructor(v: Short) { this.v = v.toInt() }
}

fun main() {
    println(C::class.primaryConstructor) // -> null
}
The
primaryConstructor
will never be
null
for a
data class
.
j
@wrongwrong I get that. In my case the class has primary constructor (its mandated by supertype). My issues is that with the ``primaryConstructor`` is when it’s accessed as part of Kotlin code base it’s there, when it’s from java it’s not… thinking about it… it makes perfect sense since Java classes probably don’t have a primary constructor :)… Thanks for being my rubber duck