[Android] Call Kotlin's default argument method from Java

If you try to call a method with Kotlin's default arguments from Java, you will get an error.

Conclusion

Annotate @JvmOverloads

Sample code

Kotlin side

class Foo {
    @JvmOverloads fun bar(str: String = "A"): String {
        return str
    }
}

Java side

Foo foo = new Foo();
foo.bar(); // "A"
foo.bar("B"); // "B"

@JvmOverloads Error when not annotating

java.lang.IllegalArgumentException: Parameter specified as non-null contains null

reference

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-overloads/index.html

Recommended Posts

[Android] Call Kotlin's default argument method from Java
Call Kotlin's sealed class from Java
Call Java method from JavaScript executed in Java
Call Java from JRuby
Java method call from RPG (method call in own class)
Call a method with a Kotlin callback block from Java
[ruby] Method call with argument
[Kotlin] Get Java Constructor / Method from KFunction and call it
[Java] Object-oriented syntax --class method / argument
Run node.js from android java (processing)
Reintroducing Java 8 available from Android Studio 2.4
Call TensorFlow Java API from Scala
Call the super method in Java
[Android, Java] Method to find the elapsed date from two dates
[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod
Java method
java (method)
Call Java library from C with JNI
Call GitHub API from Java Socket API part2
Calling java from C ++ on Android NDK
[Android / Java] Have the host Fragment (caller) call back the event from DialogFragment
Java method
Call Java methods from Nim using jnim
[Java] method
[Java] method
Increment with the third argument of iterate method of Stream class added from Java9
[Java] Dynamic method call by reflection of enum (enum)
I tried hitting a Java method from ABCL
Method name of static factory method learned from Java 8
Kotlin's reified function cannot be called from java.
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Java8 method reference
[Java] forEach method
java8 method reference
[Java] Random method
[Java] split method
About CLDR locale data enabled by default from Java 9
Call a program written in Swift from Processing (Java)