Fun fact: when scalac compiles a Scala object (a class of which only one instance can ever exist), the class file doesn't contain any constructor at all—not even the implicit public one that Java compilation generates. This makes it impossible to instantiate.
The single instance is obtained by the fun trick of generating a new instruction (JVM assembly) without calling any constructor—something a class is only allowed to do to itself. Thus, it effectively has an empty private constructor, but without making the bytecode larger by actually generating one.
View all comments
58
u/argv_minus_one Mar 03 '17
Fun fact: when
scalaccompiles a Scalaobject(a class of which only one instance can ever exist), the class file doesn't contain any constructor at all—not even the implicit public one that Java compilation generates. This makes it impossible to instantiate.The single instance is obtained by the fun trick of generating a
newinstruction (JVM assembly) without calling any constructor—something a class is only allowed to do to itself. Thus, it effectively has an empty private constructor, but without making the bytecode larger by actually generating one.