🐦 Kotlin Intermediate

What is the @JvmOverloads annotation in Kotlin?

Answer

Java does not support Kotlin's default parameter values, so a Kotlin function with 3 default parameters appears as a single overload to Java. The @JvmOverloads annotation instructs the compiler to generate additional Java-visible overloaded methods for each combination of default parameters, from the required parameters up to the full signature. This makes the function much more ergonomic to call from Java. It is commonly applied to Android View custom view constructors and any Kotlin functions with defaults that Java code needs to call.