What's the kotlin-way to create a instance dynamically?


#1

In java , we can do this:

Class.forName(“MyClass”).newInstance() ,
or Class.forName(“MyClass”).getConstructor(…).invoke(args)

Now given a data class :
  public data class MyClass (…)

How to do the same in kotlin ?
Does kotlin have it’s own reflect api ?

Thanks!

Outersky


#2

Hi, you can to exactly the same thing (modulo nullability issues that can be fixed with annotations):

Class.forName("MyClass")?.newInstance()