Scala Case Classes and Case Object

Scala case classes are just regular classes which are immutable by default and decomposable through pattern matching.

It uses equal method to compare instance structurally. It does not use new keyword to instantiate object.

All the parameters listed in the case class are public and immutable by default.


Syntax

Scala Case Class Example

Output:

a = 10
b = 10


Case classes support pattern matching. So, you can use that in patterns. Following is the example of case classes and pattern.

Scala Case Class and Pattern Matching Example

A case class which has no arguments is declared as case object instead of case class. case object is serializeable by default.

Output:

a = 10 b =10
a = 10
No Argument
Next TopicScala Constructor




Contact US

Email:[email protected]

Case Classes & Objects
10/30