Scala · Working with Data

Case Classes

Case classes are Scala’s standard way to model data. One line gives you an immutable type with everything you need.

Intermediate 5 min Lesson 10 of 25Examples use Scala 3 (3.3 LTS or newer) with sbt or scala-cli.
Before you start:Pattern Matching
0/25 in Scala
On this page · 5 min · Intermediate

In simple words#

A case class is a printed ID card: fixed fields, easy to read, two cards with the same details are “equal”, and if something changes you print a new card (copy) instead of scribbling on the old one.

case class
Immutable data type with generated methods.
copy
New instance with some fields changed.
Structural equality
Equal if all fields are equal.

Learning objectives#

  • Define case classes
  • Use copy for updates
  • Compare by value
  • Destructure in patterns

Explanation#

case class Product(name: String, price: Double) generates fields, equals, hashCode, toString, copy and an apply (no new needed).

Fields are vals — instances are immutable.

p.copy(price = 999) creates an updated instance.

Case classes work naturally with pattern matching and as Map keys.

Code example: Modelling products#

ScalaModelling products Illustrative
case class Address(city: String, pin: String)
case class Customer(name: String, email: String, address: Address)


val asha = Customer("Asha", "asha@example.com", Address("Pune", "411001"))
val moved = asha.copy(address = asha.address.copy(city = "Mumbai", pin = "400001"))


println(asha)
println(moved.address.city)
println(asha == Customer("Asha", "asha@example.com", Address("Pune", "411001")))


moved match
  case Customer(name, _, Address(city, _)) => println(s"$name lives in $city")
Expected output
Customer(Asha,asha@example.com,Address(Pune,411001))
Mumbai
true
Asha lives in Mumbai

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. No new needed — apply is generated.
  2. copy creates an updated customer; asha is unchanged.
  3. == compares all fields.
  4. Nested patterns extract the city.

Real-world use case#

API request/response models, database rows and domain events are all case classes in Scala services.

Common mistakes#

Using var fields

Breaks immutability and equality expectations. Keep fields as vals.

Case class inheritance from case classes

Not allowed/recommended. Use a sealed trait with case classes instead.

Best practices#

  • Case classes for data
  • copy for updates
  • Sealed trait + case classes for variants
  • Validate in smart constructors

Remember this#

One line = full data type.

copy, never mutate.

Equal when fields are equal.

Practice exercise#

Your task: Create a case class Book(title, price) and give a 10% discount using copy.

Show solution
Scala Illustrative
case class Book(title: String, price: Double)
val b = Book("Scala 3", 800)
val sale = b.copy(price = b.price * 0.9)

Mini quiz#

Case Classes quizMultiple choice Question 1 of 1

Which method creates a modified copy of a case class?

Score so far: 0/0

Want more? Practise all Scala MCQs · Scala interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.