← Back to Scala Course | Chapter 1: Setup & Basics | Lesson 5 of 8

Program Structure

In this page:

  1. Program Structure

Program Structure

A basic Scala program is organized as an object containing a main method (or extending App), with statements executed in order. The object Main extends App shorthand lets you write top-level statements directly in the object's body instead of wrapping them in an explicit main method. Both styles compile to a runnable program.

Note: extends App is convenient for scripts and short programs, but explicit def main(args: Array[String]): Unit is more common in larger, production Scala code.

Example: Program Structure

markup
object Main {
  def main(args: Array[String]): Unit = {
    println("Program started")
    greet()
    println("Program finished")
  }

  def greet(): Unit = {
    println("Hello from a helper method!")
  }
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.