开发者

Scala object MODULE$

What's the purpose of Scala object MODULE$?

The following Scala object:

object TestScalaObject {
 val TEST_SYMBOL = "*"
 def testMethod(x : String) : String = x
}

compiles into two bytecode files TestScalaObject.class and TestScalaObject$.class which if I disassemble to get the equivalent Java code I get:

TestScalaObject.class:

public final class TestScalaObject extends java.lang.Object{
    public static final java.lang.String testMethod(java.lang.String);
    public static final java.lang.String TEST_SYMBOL();
}

TestScalaObject$.class开发者_如何学编程:

public final class TestScalaObject$ extends java.lang.Object implements scala.ScalaObject{
    public static final TestScalaObject$ MODULE$;
    public static {};
    public java.lang.String TEST_SYMBOL();
    public java.lang.String testMethod(java.lang.String);
}

I can see a public static final TestScalaObject$.MODULE$ but what is it used for if I can access everything I need through TestScalaObject.TEST_SYMBOL and TestScalaObject.testMethod() if I ever wanted to do that from Java


MODULE$ holds an instance of the instantiated class. See the Singleton pattern in Java. I don't know of a good source for it, so here's the Wikipedia entry for Singleton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜