Scala Lesson 18 – Constructors | Dataplexa

Constructors in Scala

In this lesson, you will learn about constructors in Scala. Constructors are special parts of a class that initialize objects when they are created.

Scala provides two types of constructors:

  • Primary constructor
  • Auxiliary constructors

What Is a Constructor?

A constructor runs automatically when an object is created. It is used to initialize fields and perform setup tasks.

In Scala, constructors are tightly integrated with the class definition.


Primary Constructor

The primary constructor is defined directly in the class header.

class Person(name: String, age: Int)

Here:

  • name and age are constructor parameters
  • The constructor runs automatically when an object is created

Creating an Object Using the Primary Constructor

You create objects using the new keyword.

val p = new Person("Alice", 25)

The values are passed directly to the primary constructor.


Constructor Parameters as Fields

By default, constructor parameters are not accessible outside the class. To make them fields, use val or var.

class Person(val name: String, val age: Int)

Now name and age can be accessed from objects.

val p = new Person("Bob", 30)
println(p.name)
println(p.age)

Using Code Inside the Primary Constructor

Any code written inside the class body becomes part of the primary constructor.

class Person(val name: String, val age: Int) {
  println("Person object created")
}

This message prints every time a new object is created.


Auxiliary Constructors

Auxiliary constructors provide additional ways to create objects. They are defined using the this keyword.

class Person(val name: String, val age: Int) {

  def this(name: String) {
    this(name, 0)
  }

}

Rules for auxiliary constructors:

  • They must call another constructor using this()
  • The first statement must be a constructor call

Using Auxiliary Constructors

You can now create objects in multiple ways.

val p1 = new Person("Alice", 25)
val p2 = new Person("Bob")

The second object uses the auxiliary constructor.


Default Values in Constructors

Scala allows default values for constructor parameters. This often removes the need for auxiliary constructors.

class Person(val name: String, val age: Int = 0)

Now you can create objects with or without the age value.

val p1 = new Person("Alice", 30)
val p2 = new Person("Bob")

Private Constructors

You can restrict object creation by making constructors private.

class Database private () {
  def connect(): Unit = println("Connected")
}

This pattern is useful for singleton or controlled object creation.


Why Constructors Matter

Constructors help:

  • Initialize objects correctly
  • Enforce valid object creation
  • Improve code clarity

📝 Practice Exercises


Exercise 1

Create a class Student with name and marks.

Exercise 2

Add an auxiliary constructor that accepts only name.

Exercise 3

Use default values instead of auxiliary constructors.


✅ Practice Answers


Answer 1 & 2

class Student(val name: String, val marks: Int) {

  def this(name: String) {
    this(name, 0)
  }

}

Answer 3

class Student(val name: String, val marks: Int = 0)

What’s Next?

In the next lesson, you will learn about SBT Basics, Scala’s powerful build tool for managing projects and dependencies.