Reputation: 11
The contents of myfile.scala
are as follows :
// println("print this line")
object myObj {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
If I run : scala myfile.scala
, it prints : Hello, world
If I uncomment the first println stmt, and run : scala myfile.scala
,
it only prints : print this line
,
and does not print hello-world stmt.
Why is this so? I find it very confusing. I tried to search the archives, but could not find any answers.
Upvotes: 0
Views: 212
Reputation: 8529
When the scala command sees a top level statement (not in a class or object) in the file, it runs the file as a script, starting at the first line and moving down. You main method never gets called because you never call it, just define it. When your file doesn't contain any top level statements but it does contain a main object, it will run the main method as the entry point to the program.
Upvotes: 4