How to call Kotlin extension functions from Java code?
Hello everyone! I recently started learning Kotlin and immediately appreciated the convenience of extension functions. They allow adding new methods to existing classes, for example, to String or List.\n\nThe problem arose when I tried to use these functions in a project where part of the code is written in Java. I have a simple Kotlin file:\n\n```kotlin\n// StringExtensions.kt\npackage com.example.utils\n\nfun String.addExclamation(): String = "$this!"\n```\n\nI expected that from Java I could write something like `myString.addExclamation()`, but the compiler gives an error: "Cannot resolve method 'addExclamation'". Java simply does not see my extension function.\n\nI tried different options: imported the `StringExtensionsKt` class, made the function `@JvmStatic`, but nothing works correctly.
No answers yet.
Log in to leave an answer