September 2015
Intermediate to advanced
250 pages
6h 40m
English
Scala compiles singleton and companion objects into a “singleton class” with a special $ symbol at the end of its name. Scala, however, treats a singleton and a companion object differently, as you’ll soon see.
When compiled, a Scala singleton turns into a Java class with static methods at the bytecode level. In addition, another regular class with methods that forward calls to the singleton class is created. So, for example, this code defines a singleton object Single, and Scala creates two classes, Single$ and the forward class Single:
| Intermixing/Single.scala | |
| | object Single { |
| | def greet() { println("Hello from Single") } |
| | } |
We can use the singleton object from Java as we’d use any Java ...
Read now
Unlock full access