Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala object MODULE$

Tags:

scala

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

like image 614
dimitrisli Avatar asked Aug 04 '11 00:08

dimitrisli


1 Answers

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.

like image 102
Bradford Avatar answered Nov 09 '22 03:11

Bradford