How to instantiate inner classes in one step in Scala?
First of all, I doubt that the instantiation in one go is any meaningful -- you are like throwing away the Outer
instance, keeping no reference to it. Makes me wonder, if you weren't thinking of a Java static inner class, like
public class Outer() {
public static class Inner() {}
}
which in Scala would translate to Inner
being an inner class of Outer
's companion object:
object Outer {
class Inner
}
new Outer.Inner
If you really want an inner dependent class, and you just want more convenient syntax for instantiating it, you could add a companion object for it:
class Outer {
object Inner {
def apply() = new Inner()
}
class Inner
}
new Outer().Inner()