i have a pretty long questions about the different...
# announcements
t
i have a pretty long questions about the different ways of calling a java method from kotlin, so i'm going to nest it into a thread
Copy code
say i have a dummy method:
myMethod(Handler handler) { // Handler is a void->void @FunctionalInterface
    ...
}
in java, i can create non-anonymous handlers in three different ways:
Copy code
MyClass {
    Handler fieldHandler = () -> {};
    methodRefHandler(Handler handler) {}
    ClassHandler implements Handler {}
}
and call myMethod like this:
Copy code
myMethod(MyClass.fieldHandler);
myMethod(MyClass::methodRefHandler);
myMethod(new ClassHandler());
what i'm wondering is, what are all the possible ways of calling myMethod (the java version), from kotlin
k
Well. You could do the exact same things in Kotlin. By the way, for the sake of completeness, you missed anonymous instance of Handler, and using a lambda directly, which also works in both languages 🙂
t
i forgot to specify i'm only looking for non-anonymous instances (fixed now)
k
Yea. It’s more or less the same options you have in KT as well
t
i know i can do the same in kotlin, but there are more options? you can do it via objects, companion objects, standalone fields, etc.. 🤔
k
Object/class/companion objects would be implementations, the same as your
new ClassHandler()
Fields/properties, standalone/within a class, would be the same as your
MyClass.fieldHandler
Functions/methods, bound or unbound, will be the same as your
MyClass::methodRefHandler
I can’t think of any other different way to achieve this asides what’s already mentioned
t
alright, thanks!
thought there would be more options, but maybe not