Attribution-NonCommercial-ShareAlike 4.0 International

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

Scala Tutorial

This page gives a very brief tutorial on the Scala programming language.

Classes, Traits, Objects, and Packages


Classes in Scala are very similar to classes in Java. They are templates containing fields and methods. Like in Java, classes can be instantiated using the new construct, there can be many “instances” (or “objects”) of the same class.

In Scala, there exists a special kind of class named case classes. You will learn about case classes during the course.

Classes in Scala cannot have static members. You can use objects (see below) to achieve similar functionality as with static members in Java.


Traits are like interfaces in Java, but they can also contain concrete members, i.e., method implementations or field definitions.


Objects in Scala are like classes, but for every object definition, there is only one single instance. It is not possible to create instances of objects using new, instead you can just access the members (methods or fields) of an object using its name.


Adding a statement such as package at the top of a file makes the code in a file part of the package You can then do import to make everything from the package available in your code. The content of a package can be scattered across many files. If you define a class MyClass in package, you can import that specific class (and not anything else from that package) with import

In Scala, everything can be imported, not only class names. So for instance if you have an object baz in package, then import would import all the members of that object.

Hello, World! in Scala

Here are two ways to define a program that outputs “Hello, World!” in Scala:

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


object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, World!")

In Scala, the main or entry point method is defined in an object. An object can be made executable by either adding or extending the type App or by adding a method def main(args: Array[String]).

Source Files, Class files, and the JVM

Scala source code is stored in text files with the extension .scala. The Scala compiler compiles .scala source files to .class files, like the Java compiler. Class files are binary files containing machine code for the Java Virtual Machine. In order to run a Scala program, the JVM has to know the directory where class files are stored. This parameter is called the “classpath”.

If you are using IntelliJ, VS Code, or sbt to compile and run your Scala code, you don’t need to do any of the above manually – these tools take care of invoking the Scala compiler and the JVM with the correct arguments.