Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.forName with instance checks in Kotlin

Tags:

kotlin

Is it possible to use instance checks (i.e. x is String checks in Kotlin) when using Class.forName()? I want to apply this to a class that is only available at runtime, not at compile time.

MWE:

val x = "banana"
val specialClass = Class.forName("org.company.specialclass.at.runtime")

x is specialClass

However, I get unresolved reference on specialClass. Any ideas?

like image 900
Robin Trietsch Avatar asked Oct 29 '25 13:10

Robin Trietsch


1 Answers

The function isInstance may help:

Class.forName("java.lang.String").isInstance("txt") //true

Here you can find the documentation for isInstance.

like image 112
s1m0nw1 Avatar answered Oct 31 '25 12:10

s1m0nw1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!