Scala Case Class

We learned about class in a previous SKB, today we are going to learn about case class. They serve the same purpose which is to provide a blueprint to create objects. But case class provides a lot of build-in advantages.

As a start, notice how the case class is being instantiated, and how the fields are being accessed.

Did you notice that we do not need new to create the instance of the class?

And did you notice that we do not need a function to access any of the fields?

In Scala, case class provide build-in things, let’s go over them:

  • Constructor: To instantiate an object from a case class, no need for the new keyword. Note that class will not need new either in Scala 3. To understand how this is done, remember the words apply and Companion Object, we are going to come back to it later.
  • Field accessors: in case class, the field are public ( we are going to talk about visibility later ) by default. So you can access their values without the need to a method that will return it for you.
  • And more; remember the terms unapply, Product and Serialization for later SKBs.

Reveal more information and clues
Load Exercise