0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Case Classes

Immutable data carriers.

What Is a Case Class?

A case class is a special kind of class designed to hold immutable data. You declare it with case class.

Scala generates a lot of useful code for you automatically, making case classes the go-to way to model data.

Declaring One

Just list the fields in the parameter list. Each parameter becomes an immutable field.

No new keyword is required to create an instance.

case class Point(x: Int, y: Int)
object Main {
  def main(args: Array[String]): Unit = {
    val p = Point(3, 4)
    println(p.x)
    println(p.y)
  }
}

All lessons in this course

  1. Case Classes
  2. Sealed Traits
  3. Algebraic Data Types
  4. Exhaustive Matching
← Back to Scala for Backend Engineering & Functional Programming