Scala Language Hello World by extending App


Example

object HelloWorld extends App {
  println("Hello, world!")
}

Live demo

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".

2.11.0

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.

2.11.0

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 for App 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.