In Scala, we can write
object Foo { def bar = {} }
How is this implemented by the compiler? I am able to call Foo.bar();
from Java
but new Foo();
from Java gives the error cannot find symbol symbol: constructor Foo()
- Does the JVM support singletons natively?
- Is it possible to have a class in Java that does not have a constructor?
Note: here is the code output by scalac -print
package <empty> {
final class Foo extends java.lang.Object with ScalaObject {
def bar(): Unit = ();
def this(): object Foo = {
Foo.super.this();
()
}
}
}
ensure only 1 instance
of theSingleton
is created. – Feeler