Static inner classes in scala

19,384

Solution 1

You can do something like this if don't need access to the outer class in the inner class (which you wouldn't have in Java given that your inner class was declared static):

object A{
    class B {
        val x = 3
    }
}
class A {
    // implementation of class here
}
println(new A.B().x)

Solution 2

As others have pointed out, "static" classes should be placed inside the companion object.

In Scala, classes, traits, and objects which are members of a class are path-dependent. For example:

class Button {
  class Click
}


val ok = new Button
val cancel = new Button

val c1 = new ok.Click
val c2 = new cancel.Click

Now c1 and c2 are instances of -different- classes. One class is ok.Click, and the other is cancel.Click. If you wanted to refer to the type of all Click classes, you could say Button#Click.

Solution 3

From scala-lang:

there is no notion of 'static' members in Scala. Instead, Scala treats static members of class Y as members of the singleton object Y

So it seems you could have a class defined inside an Object, but not a static class defined inside a class.

Solution 4

Not sure I fully understood your use case... If it can help you, objects inside classes are visible like an instance's fields, e.g.

case class C(var x: Int)
class A { case object b extends C(0) }
val a = new A
println(a.b.x)
a.b.x = 2
println(a.b.x)

Moreover, you can perfectly override a parent's val with object:

case class C(var x: Int)
class A { val y: C = C(0) }
class B extends A { override case object y extends C(2) }
val a: A = new B
println(a.y.x)

Solution 5

In scala if you need to create a some static methods you can use a companion object, with the same name of the class, where you store all the pseudo static methods. Ex:

class A {
}

object A {
    def xpto // define some pseudo static methods here..
}

Then you can just use A.xpto.
Try to read more about companion modules on scala

Share:
19,384
oxbow_lakes
Author by

oxbow_lakes

Currently programming in scala and Java at GSA Capital, a multiple-award-winning quantitative investment manager. Experience: Scala (since 2008) Java (since 1999) SQLServer git

Updated on June 02, 2022

Comments

  • oxbow_lakes
    oxbow_lakes about 2 years

    What is the analog in Scala of doing this in Java:

    public class Outer {
      private Inner inner;
    
      public static class Inner {
      }
    
      public Inner getInner() { return inner; }
    }
    

    I specifically want my inner class to not have to have a fully qualified name - i.e. I want Trade.Type, not TradeType. So in Scala I imagined it might be something like:

    class Outer(val inner: Inner) {
        object Inner
    }
    

    But this doesn't seem to work: my scala Inner just doesn't seem to be visible from outside the Outer class. One solution would of course be:

    class Inner
    class Outer(val inner: Inner)
    

    Which is OK - but because of the names of my classes, Inner is really the "type" of the Outer and Outer actually has a long name. So:

    class SomeHorriblyLongNameType
    class SomeHorriblyLongName(myType: SomeHorriblyLongNameType)
    

    Which is verbose and horrible. I could replace SomeHorriblyLongNameType with just Type but there would then be no obvious connection between it and the class it was related to. Phew

  • oxbow_lakes
    oxbow_lakes almost 15 years
    If you declare an object inside a class in Scala, it is then not visible from outside that class (or I couldn't figure out how to make it so). Your answer is not quite what I was asking: I was asking how I could do something in Scala, as opposed to asking how Scala would treat Java statics
  • Dmitry Zaytsev
    Dmitry Zaytsev over 11 years
    This class is inner but not static