Outdated Notice
You define auxiliary Scala class constructors by defining methods that are named this
. There are only a few rules to know:
- Each auxiliary constructor must have a different signature (different parameter lists)
- Each constructor must call one of the previously defined constructors
Here’s an example of a Pizza
class that defines multiple constructors:
val DefaultCrustSize = 12
val DefaultCrustType = "THIN"
// the primary constructor
class Pizza (var crustSize: Int, var crustType: String) {
// one-arg auxiliary constructor
def this(crustSize: Int) = {
this(crustSize, DefaultCrustType)
}
// one-arg auxiliary constructor
def this(crustType: String) = {
this(DefaultCrustSize, crustType)
}
// zero-arg auxiliary constructor
def this() = {
this(DefaultCrustSize, DefaultCrustType)
}
override def toString = s"A $crustSize inch pizza with a $crustType crust"
}
With all of those constructors defined, you can create pizza instances in several different ways:
val p1 = new Pizza(DefaultCrustSize, DefaultCrustType)
val p2 = new Pizza(DefaultCrustSize)
val p3 = new Pizza(DefaultCrustType)
val p4 = new Pizza
We encourage you to paste that class and those examples into the Scala REPL to see how they work.
Notes
There are two important notes to make about this example:
- The
DefaultCrustSize
andDefaultCrustType
variables are not a preferred way to handle this situation, but because we haven’t shown how to handle enumerations yet, we use this approach to keep things simple. - Auxiliary class constructors are a great feature, but because you can use default values for constructor parameters, you won’t need to use this feature very often. The next lesson demonstrates how using default parameter values like this often makes auxiliary constructors unnecessary:
class Pizza(
var crustSize: Int = DefaultCrustSize,
var crustType: String = DefaultCrustType
)
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next