How to define a property using get() in Kotlin which returns a class, I was trying below, but it's not compiling
val targetActivity: Class<?>
get() = MyActivity.class
How to define a property using get() in Kotlin which returns a class, I was trying below, but it's not compiling
val targetActivity: Class<?>
get() = MyActivity.class
You can use Class References
The most basic reflection feature is getting the runtime reference to a Kotlin class. To obtain the reference to a statically known Kotlin class, you can use the class
literal syntax:
val c = MyClass::class
or this use Class<*>
instead of Class<?>
val targetActivity: Class<*>
get() = MyActivity::class
Be aware that in Kotlin you have to use star projection, the question mark <?>
won’t work; also use class references like this:
val targetActivity: KClass<*>
get() = MyActivity::class
If you want to have a Java Class
, use the .java
property: MyActivity::class.java
you need to use .java after getting a Kotlin KClass to return a Java Class
val targetActivity: Class<*>
get() = MyActivity::class.java
Or, if you want to be more specific about the return type
val targetActivity: Class<MyActivity>
get() = MyActivity::class.java