Case Classes
Case classes are Scala’s standard way to model data. One line gives you an immutable type with everything you need.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
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#
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")
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#
- No new needed — apply is generated.
- copy creates an updated customer; asha is unchanged.
- == compares all fields.
- 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#
Breaks immutability and equality expectations. Keep fields as vals.
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
case class Book(title: String, price: Double)
val b = Book("Scala 3", 800)
val sale = b.copy(price = b.price * 0.9)
Mini quiz#
Which method creates a modified copy of a case class?
Want more? Practise all Scala MCQs · Scala interview questions
My notes#
Saved in this browser.