Scala Language Getting started with Scala Language Hello World by extending App

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Scala Language Question?