It seems that scala compiles methods in companion objects into static methods, which makes it easier to access them from java code. For example, you can write CompanionObject.method () instead of CompanionObject $ .MODULE $ .method (). However, sometimes a seemingly inappropriate code change violates this behavior. I came up with this example to illustrate the problem.
$ cat TestCompanion.scala class TestCompanion object TestCompanion { def init2 {} } @SerialVersionUID(1L) class TestCompanion2 object TestCompanion2 { def init2 {} } $ scalac -version Scala compiler version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL $ scalac TestCompanion.scala $ javap TestCompanion Compiled from "TestCompanion.scala" public class TestCompanion extends java.lang.Object implements scala.ScalaObject{ public static final void init2(); public TestCompanion(); } $ javap TestCompanion2 Compiled from "TestCompanion.scala" public class TestCompanion2 extends java.lang.Object implements scala.ScalaObject{ public static final long serialVersionUID; public static {}; public TestCompanion2(); }
So the only difference between TestCompanion and TestCompanion2 is that the latter is annotated using @SerialVersionUID, and init2 is compiled into a static method in TestCompanion, but not in TestCompanion2.
Can someone explain why scalac treats these two classes differently? I do not see how the @SerialVersionUID annotation should affect the availability of static methods.
source share