What's the correct way to instantiate a class usin...
# announcements
a
What's the correct way to instantiate a class using the reflections (KClass) with primary constructor? Assuming we know the type and number of parameters.
Using the constructors:
Copy code
MyClass::class.constructors.first().call(5)
Copy code
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
Following error occurs, do we need to add dependency to the kotlin-reflect for this? Is there any other way, in the standard libarary itself.
v
If you want to use reflection, you need the code for it. I don't think there is another way.
a
Though I found,
::MyClass
actually gives primary constructor reference, so it works 😛