https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Kwok Wong

12/01/2023, 5:45 PM
Hi, basic Kotlin Reflection question to call a static function. I have a function test() defined in Test.kt:
Copy code
package com.test.test

fun test() {
    // Do some stuff
}
How can I call the function test() using Kotlin reflection
h

hho

12/01/2023, 7:12 PM
Just like in Java:
Copy code
Class.forName("com.test.test.TestKt").getDeclaredMethod("test").invoke(null);
The
TestKt
comes from your filename
k

Kwok Wong

12/02/2023, 9:16 AM
Yes, I thought that I need to use "`KClass*.companionObject" to invoke the static method.` Indeed just do it with the Java way, thanks!
13 Views