Case Class

Scala Comments

In this blog post, you’ll learn about -

  1. What benefits do case classes provide and when you should use them.

Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments.

When you define a case class, the following things happen automatically -

  1. Each of the constructor parameters are declared as a val and you’ve getters and setters for each of them.

  2. An apply method is provided - this would let you create new objects using the new keyword.

  3. Methods toString, equals, copy and hashCode are generated - this would help you serialization, deserialization, comparison and copying objects.

  4. Case classes are also optimized for use in Pattern Matching.

All these benefits are seen in the sample code below -

Kaushik Rangadurai

Code. Learn. Explore

Share this post