Is there a defined difference between `this::class...
# announcements
e
Is there a defined difference between
this::class.java
and
javaClass
in regards to generics? If I have this class defined:
Copy code
class Test<T>(
    val me: Class<T>
)
And I instantiate it like this, it's fine:
Copy code
fun <T> T.test() {
  Test(javaClass)
}
However, if I instantiate it like this, it won't compile:
Copy code
fun <T> T.test() {
  Test(this::class.java)
}
And the error is:
Type inference failed. Expected type mismatch:
Required: Test<T>
Found: Test<out T>
h
.javaClass
is the equivalent of
getClass()
in Java.
::class.java
is the equivalent of
.class
in Java. So what you're trying to do is
this.class
– and that won't work, just as in Java.
👍 2