Why are we writing NextActivity :: class.java even though it is a kotlin class?

We need to write this line with the extension .java, although its extension is .kt I think the Kotlin file is converted to a java file, but java is also converted to byte code, so we can also use a .class file. If the Kotlin code is converted to Java code.

NextActivity::class.java before NextActivity::class.kt //not worked

btn?.setOnClickListener {
   startActivity(Intent(this, NextActivity::class.java))
}

So the question is why we are writing .java in NextActivity::class.java

The question arises from here .

+4
source share
2 answers

Because you want to access Java Class methods .

, Kotlin, , , "" kotlin Java.

NextActivity::class KClass, KClass java Intent contructor Intent(Context packageContext, Class cls), Class type , , , .

+3

Java- Kotlin. Intent - Java- Android.

Doc: https://kotlinlang.org/docs/reference/reflection.html#class-references

, Kotlin Java . Java, .java KClass.

Kotlin Java (Intent ), Java.

interop doc: https://kotlinlang.org/docs/reference/java-interop.html#java-reflection

. , instance:: class.java, ClassName:: class.java instance.javaClass, Java java.lang.Class.

+4

Source: https://habr.com/ru/post/1683401/


All Articles