Ondrej Sotolar
Ondrej Sotolar

Reputation: 1442

Java inner class import to Scala project

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.

Upvotes: 1

Views: 1476

Answers (3)

dacwe
dacwe

Reputation: 43504

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

Upvotes: 10

Alexey Romanov
Alexey Romanov

Reputation: 170723

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

Upvotes: 4

ruakh
ruakh

Reputation: 183270

According to Iulian Dragos, who would know,

There is indeed no syntax to import Outer#Inner.

(link)

Upvotes: 1

Related Questions