Constructors and Auxiliary Constructors

Scala has a primary constructor defined in the class signature, and optional auxiliary constructors using def this().

Primary Constructor

class Book(val title: String, val author: String)

Auxiliary Constructor

class Book(val title: String, val author: String) {
  var year: Int = 0
  def this(title: String, author: String, year: Int) = {
    this(title, author)
    this.year = year
  }
}
← PrevNext →