Scala Tutorial - Scala Case Classes

Scala can create classes that have the common stuff filled in.

Most of the time, when we define a class, we have to write the toString, hashCode, and equals methods.

Scala provides the case class mechanism for filling in these blanks, as well as support for pattern matching.

A case class provides the same facilities as a normal class, but the compiler generates toString, hashCode, and equals methods which you can override.

Case classes can be instantiated without the use of the new statement.

By default, all the parameters in the case class's constructor become properties on the case class.


Here's how to create a case class:

case class Stuff(name:String, age: Int)

We can create an instance of Stuff without the keyword new:

vals = Stuff("David", 45)
s: Stuff = Stuff(David,45)

Call the case class's to String method:


Stuff's equals method does a deep comparison:

s == Stuff("David",45)
s == Stuff("David",43)

And the instance has properties: