Scala对象MODULE$的目的是什么?
以下是Scala对象:
object TestScalaObject {
val TEST_SYMBOL = "*"
def testMethod(x : String) : String = x
}
编译成两个字节码文件TestScalaObject.class和TestScalaObject$.class,如果我反汇编获取等效的Java代码,则会得到:
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);
}
我可以看到一个public
static
final
TestScalaObject$.MODULE$,但是如果我需要从Java中访问我需要的所有内容,通过TestScalaObject.TEST_SYMBOL和TestScalaObject.testMethod(),那它有什么用呢?