object HelloWorld extends App {
println("Hello, world!")
}
By extending the App
trait, you can avoid defining an explicit main
method. The entire body of the HelloWorld
object is treated as "the main method".
Delayed Initialization
Per the official documentation,
App
makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.
Delayed Initialization
Per the official documentation,
App
makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.
DelayedInit
is now deprecated for general use, but is still supported forApp
as a special case. Support will continue until a replacement feature is decided upon and implemented.
To access command-line arguments when extending App
, use this.args
:
object HelloWorld extends App {
println("Hello World!")
for {
arg <- this.args
} println(s"Arg=$arg")
}
When using App
, the body of the object will be executed as the main
method, there is no need to override main
.