
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Extending a Class
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Currying Functions
- Scala Collections
- Scala - Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
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 -
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 -
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 -
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.