Creating Executable Programs with the App Trait



This chapter takes you through the concept of application objects in Scala programming. Application objects can create executable programs without the main method in a class.

Application Objects

Application object is an object that extends the App trait. This trait can create an executable Scala application. You can place the code directly in the body of the object.

Application object is an object that extends the App trait. So, you can write the code in the object body instead of within a main method.

Syntax

The syntax of application object without need of main entry −

object ObjectName extends App { // Program code }

Example

The following example shows a simple application object in Scala programming -

Open Compiler
object Demo extends App { println("Hello, Scala!") }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

Hello, Scala!

In the example, the Demo object extends the App trait. The println statement is placed directly in the object body. It executes when the program runs.

Command-Line Arguments

When using the App trait, you can access command-line arguments through the args array. It is inherited from the App trait.

Syntax

The syntax of command line argument code -

object ObjectName extends App { // Access command-line arguments args.foreach(arg => println(arg)) }

Example

The following example shows to handle command-line arguments in an application object -

object Demo extends App { if (args.length == 0) { println("No command-line arguments provided.") } else { println("Command-line arguments:") args.foreach(arg => println(arg)) } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo arg1 arg2 arg3

Output

Command-line arguments:
arg1
arg2
arg3

In the example, the Demo object extends the App trait. It checks if any command-line arguments are provided. If arguments are present, it prints each one. The args array, inherited from the App trait, holds the command-line arguments.

Application Object with Initialization Code

You can include initialization code in an application object. You can place it directly in the object body. This code runs when the application starts.

Syntax

The syntax of the application object with initialization code is -

object ObjectName extends App { // Initialization code println("Initialization code executed.") }

Example

The following example shows an application object with initialization code in Scala -

Open Compiler
object Demo extends App { println("Application started.") // Initialization code for (i <- 1 to 5) { println(s"Initialization step $i") } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo arg1 arg2 arg3

Output

Application started.
Initialization step 1
Initialization step 2
Initialization step 3
Initialization step 4
Initialization step 5

Application Object with Methods

You can define methods within an application object. It encapsulates functionality and organizes your code better.

Syntax

The syntax of application object with methods is -

object ObjectName extends App { // Method definition def methodName(): Unit = { // Method body } // Method invocation methodName() }

Example

The following example shows an application object with a method in Scala programming -

Open Compiler
object Demo extends App { // Method definition def greet(name: String): Unit = { println(s"Hello, $name!") } // Method invocation greet("Scala Developer") }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo arg1 arg2 arg3

Output

Hello, Scala Developer!

In the example, the Demo object extends the App trait and defines a greet method. This method is then invoked in the object body. It shows how to organize code within an application object.

Application Objects Summary

  • Application objects in Scala provide a convenient way to create executable programs by extending the App trait.
  • You can place the code directly in the object body. So, there is no need to define a main method.
  • Application objects can handle command-line arguments through the inherited args array.
  • Initialization code can be included directly in the object body, and methods can be defined to organize functionality.
Advertisements