1

I want to import java inner class into Scala project. The code, which wouldn't compile looks like this:

import pac.Obj
import pac.Obj.Inner.Inner2

object Sample {
    def main(args: Array[String]): Unit = {            
        var o = new Obj()
        Inner2 i2 = o.getInner().addInner2("some text")
    }
}

The scala compiler is unable to recognize the second import. Why is that? In Java, this construct works fine.

3 Answers 3

10

It does work if the Inner class is static.

If it isn't, well you're out of luck (but do you really need it?). But you can use the name with the # separator like this:

var inner = outer.getInner : Outer#Inner
Sign up to request clarification or add additional context in comments.

Comments

4

Inner2 i2 is illegal in Scala in any case, and val i2 = o.getInner().addInner2("some text") will work fine.

Comments

1

According to Iulian Dragos, who would know,

There is indeed no syntax to import Outer#Inner.

(link)

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.