Kotlin val not reachable form Java


#1

Kotlin class:

But when I instance a ScriptKt class from java, the fields for f, rlist and d aren’t available. I was expecting something like:

ScriptKt script = new ScriptKt();
script.getFfield() // Why isn't this here?  Compile error and doesn't show up in code complete.

What am I doing wrong?


#2

You’ve applied the @JvmField annotation to the properties. A property with that annotation is compiled directly to a Java field, and the corresponding getter and setter are not generated. You can access such a property as script.ffield.


#3

That isn’t available either.


#4

ScriptKt is the facade class for top-level declarations declared in script.kt. A Kotlin class named Script is referenced from Java as simply Script.


#5

I’m confused. So should I reference my kotlin “Script.kt” class from Java as “Script” like:

Script script = new Script(); 

#6

Script.kt is not a class, it’s the name of a file. A class called Script should be referenced from Java as new Script(), indeed.


#8

That worked. Thank you!