Declaring actual val in Java

I am working on the KMP library, which should be called from multiple platforms.

Starting with multiplatform-library-template, I try to add Java code to call CommonMain Kotlin function.

The CommonMain Kotlin code also declares:

expect val firstElement: Int
expect val secondElement: Int

How to declare actual values in Java code (in JvmMain module)?

Write exactly the same line, replacing expect by actual, in src/jvmMain/kotlin. The package declaration at the top of the file should be exactly the same, but the file doesn’t have to be named the same.

In Kotlin – I understand. How I can do this in Java?
I need the Java developer, using this library from Java application, to be able to provide actual for these val’s

You cannot write it directly in Java. However, you can implement them by calling a Java method.

class JavaGenerator {
    static int generateFirstElement() {
actual val firstElement: Int = JavaGenerator.generateFirstElement()
1 Like