Reputation: 1849
When we compile scala code using
scalac code.scala
There are two files created named code.class
and code$.class
.
What is the purpose of this second file?
For reference,
object HelloWorld {
def main (args: Array[String]) {
println ("Hello world! This is my first scala program!");
}
}
This is the code inside code.scala
file.
Upvotes: 4
Views: 2041
Reputation: 3081
This is the consequence of how Scala translates the Scala object
to JVM constructs. An object X
is translated to a class X$
with ordinary methods, ordinary inheritance etc.
As object X
creates singleton in Scala, it would be nice to be able to access its public methods from Java by calling them as static methods X.someMethod()
. That is why the Scala compiler creates also a class X
with static methods calling the ordinary methods of the singleton instance of the class X$
. Even if you don't have the Scala class X
itself.
Upvotes: 8
Reputation: 931
Second file is for companion object, if you have both
class Code{}
object Code{}
in your file
Upvotes: 0