I have a Java method that takes a Method
parameter:
void doSomethingWithMethod(Method m) {
...
}
And I have a Kotlin class that contains a function:
class MyClass {
fun myFunction() : List<Something> {
...
}
}
I can get a reference to the function with MyClass::myFunction
, but I don't see a way to pass it into the doSomethingWithMethod
method. Is there an equivalent to the .java
property that can be applied to a Kotlin class reference to get its Java equivalent?
If not, is there a workaround?
Calling Kotlin from Java: Package-level functions The arguments of a function may have default values. This means that if we don't specify them, they'll take the value specified in the declaration. This prevents us from using method overloading, as we tend to use in Java. The function is applied over a ViewGroup .
Since no assignment is possible for function parameters, the main advantage of passing by reference in C++ does not exist in Kotlin. If the object (passed to the function) has a method which changes its internal state, it will affect the original object. No copy is ever generated when passing objects to functions.
Even if it's interop you can't mix Java and Kotlin in the same file. If you really want to have static methods/variables you can use an companion object . You can also access create a "real" static method in your JVM by using @JvmStatic . By using @JvmStatic you can use Java to access your Static methods like before.
import kotlin.reflect.jvm.javaMethod
val method = MyClass::myFunction.javaMethod
The javaMethod
property is not part of the standard Kotlin library, but is part of the official kotlin-reflect.jar
. It can be added through Maven with the following dependency:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With