0

I have the following scala example:

class Nested {}
object Nested {
  class Inner {}
  object Inner {
    def x = 321
  }
}

With a JUnit test to test specifically that I can do Nested.Inner.x() and call the method freely without the Nested.Inner$.MODULE$.x():

  import static junit.framework.Assert.*;
  import org.junit.Test;

  public class TestFromJava {

  @Test
  public void accessTest() {
    assertEquals(321, Nested.Inner.x());
  }
}

This compiles, but at the moment of running the test, I am getting (sbt test):

[error] TestFromJava.java:8: cannot find symbol
[error]   symbol:   method x()
[error]   location: class Nested.Inner
[error] Nested.Inner.x

How can I benefit from both Scala syntax and not using the horrible $? Maybe it is a feature of the language as how it generates the object instances.

6
  • What do you mean by "this compiles"? Is that a runtime error you are showing? Looks like you are using sbt. Did you succeed to run sbt test:compile? I think you pretty much do need to access that symbol through Nested.Inner$.MODULE$.x() Commented Feb 29, 2016 at 20:49
  • Compiles meaning sbt clean compile succeeds. Commented Feb 29, 2016 at 20:58
  • But where is TestFromJava, is that main sources or test sources? Is that in src/main/java or src/test/java (assuming you have an sbt build - can you confirm this?). compile does not compile test-sources, it's short for compile:compile. You need to run test:compile. Commented Feb 29, 2016 at 21:03
  • Which scala version is this? If I recall you don't need the $MODULE for 2.11. Commented Feb 29, 2016 at 21:45
  • @0__ on sbt test:compile it complains about the same compilation error from above. @yw3410 2.11.7 scala version Commented Mar 1, 2016 at 12:41

1 Answer 1

1

You have to qualify singleton object access when calling from Java, as explained in this question.

In your case that would be

Nested.Inner$.MODULE$.x()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.