When are singleton objects built?
2 answers
The code will be called when O
opened for the first time (some method or some property). For example, the following program
object O {
println("Hello from O")
def doSome() {}
}
object App extends Application {
println("Before O")
O.doSome()
println("After O")
}
will give
Before O
Hello From O
After O
It is not easy enough to define O
. Also the call won't work Class.forName("O")
as the name of the compiled object O$
, so call Class.forName("O$")
.
+12
a source to share