I have an object within another object in Scala which I want to import in Java. The setup is something like this:
Scala:
package scalapart;
object Outer {
object Inner {
def method: String = "test"
}
}
Java:
import scalapart.Outer.Inner;
...
Outer.Inner.method()
Unfortunately, I get cannot find symbol in line 1 of the Java code. And if I import scalapart.Outer;, then the second line reports the same issue.
Is there a way to import a Scala object that is defined inside another Scala object in Java?
You can either used it directly:
Or do a
static importand use it like this (or similar):Java doesn’t have any direct equivalent to the singleton object, so for every Scala singleton object, the compiler creates a synthetic Java class with the same name plus a dollar sign appended at the end) for that
objectand astaticfield namedMODULE$to hold the single instance of the class. So, to ensure there is only one instance of an object, Scala uses a static class holder.Your disassembled code looks something like this: