Scala Constructor Arguments and what difference does it make

Reading Time: 3 minutes

Introduction to Scala constructors

Scala constructors might feel a little unusual especially if we come from a more verbose language. Constructors in Scala are of 2 types:

  1. Primary Constructor
  2. Auxiliary Constructor

This is how we declare a primary constructor in Scala:

class <class_name>(args...)

In this blog, we will see different ways to pass arguments in the primary constructor and how it affects their visibility.

Passing arguments in Scala constructor

There can be mainly three ways in which we can pass arguments to constructors:

1. Prefixing the keyword val before argument name

class Book(val name: String, val typeOfBook: String)

2. Prefixing the keyword var before the argument name

class Book(var name: String, var typeOfBook: String)

3. Directly declaring arguments without using any keyword

class Book(name: String, typeOfBook: String)
Creation of Book class
In all the 3 cases, a class Book is created

1. Using val keyword

val keyword adds immutability to the particular variable in general. Adding val keyword to the field makes it a read-only field. In this case, the Scala compiler automatically adds accessor or getter methods.

A similar Java code would look like this:

/* The following getter or accessor method is added by the Scala compiler automatically */
    public String getName() {
        return this.name
    }

As we can see, Scala saves us from writing a lot of code. We can access the fields directly using the .’ operator like this:

Accessing val fields in Scala

2. Using var keyword

var keywords are used to create mutable variables in Scala. Adding var keyword to the field directs the Scala compiler to automatically add accessor and mutator methods for that field.

An equivalent java code will look like the following:

/* The following getter or accessor method is added inside the Book class by the Scala compiler automatically */
    public String getName() {
        return this.name
    }

/* The following setter or mutator method is added inside the Book class by the Scala compiler automatically */
    public void setName(String name) {
        this.name = name
    }

In Scala, the ‘.‘ operator allows us not just to access the fields but also to mutate them.

Accessing and Mutating name field of Book class in  Scala

3. Using neither val nor var

If we declare constructor arguments without using val or var then the compiler treats them simply as constructor parameters and does not generate any field, accessor, or mutator methods. Hence, its visibility becomes very restricted.

Constructor parameters without val or var in Scala

Case Classes

When we use case classes, there is a slight variation to the above rule. If we do not provide var or val keyword explicitly then, by default, a val keyword is added and hence we can access the values but cannot change it.

Constructor parameters for Case classes in Scala

Summary

So in this blog, we have discussed the effects of using val and var keywords in constructor arguments. The blog also briefly discusses what’s different in case classes. The below table summarises the contents of the blog:

Keywords and their visibility

References: https://docs.scala-lang.org/

For more info: https://blog.knoldus.com/

knoldus