0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Case Classes

Immutable data carriers.

Case Classes is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
  }
}

Automatic toString

Case classes get a readable toString for free, showing the class name and field values.

This makes debugging and logging effortless.

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

Value Equality

Case classes compare by value, not by reference. Two instances with the same fields are equal.

Regular classes would compare by identity instead.

case class Point(x: Int, y: Int)
object Main {
  def main(args: Array[String]): Unit = {
    val a = Point(1, 2)
    val b = Point(1, 2)
    println(a == b)
  }
}

Immutable Fields

Case class fields are val by default, so they cannot be reassigned.

To get a modified version, you create a new instance instead of mutating the old one.

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

The copy Method

Every case class has a copy method. It creates a new instance, letting you change just the fields you name.

This is the idiomatic way to update immutable data.

case class Point(x: Int, y: Int)
object Main {
  def main(args: Array[String]): Unit = {
    val p = Point(1, 2)
    val moved = p.copy(y = 99)
    println(p)
    println(moved)
  }
}

Pattern Matching Support

Case classes work seamlessly with match. You can deconstruct them by naming their fields in a pattern.

This is one of the biggest reasons to use them.

case class Point(x: Int, y: Int)
object Main {
  def describe(p: Point): String = p match {
    case Point(0, 0) => "origin"
    case Point(x, 0) => s"on x-axis at $x"
    case Point(x, y) => s"point ($x, $y)"
  }
  def main(args: Array[String]): Unit = {
    println(describe(Point(0, 0)))
    println(describe(Point(5, 0)))
    println(describe(Point(2, 3)))
  }
}

Default Parameter Values

Like normal classes, case classes can have default values for parameters, so you can omit them when constructing.

case class User(name: String, active: Boolean = true)
object Main {
  def main(args: Array[String]): Unit = {
    val u1 = User("Ada")
    val u2 = User("Bo", false)
    println(u1)
    println(u2)
  }
}

Adding Methods

A case class can have its own methods in the body, just like any class. They operate on the immutable fields.

case class Rect(w: Int, h: Int) {
  def area: Int = w * h
}
object Main {
  def main(args: Array[String]): Unit = {
    val r = Rect(3, 4)
    println(r.area)
  }
}

Why Case Classes?

Case classes give you, for free:

  • A concise constructor with immutable fields
  • Readable toString
  • Value-based equals and hashCode
  • A copy method
  • First-class pattern matching

They are the foundation for modeling data in Scala.

Putting It Together

Here a case class is created, copied, compared, and pattern matched in one example.

case class Money(amount: Int, currency: String)
object Main {
  def main(args: Array[String]): Unit = {
    val price = Money(10, "USD")
    val sale = price.copy(amount = 8)
    println(price == sale)
    sale match {
      case Money(a, "USD") => println(s"$a dollars")
      case Money(a, c)     => println(s"$a $c")
    }
  }
}

Quick Check

Test your understanding of case classes.

Recap

You learned that a case class models immutable data with generated helpers:

  • Construct without new; fields are immutable vals
  • Free toString, value-based equals/hashCode
  • copy creates modified clones
  • Built-in support for pattern matching
  • Default values and custom methods are allowed

Frequently asked questions

Is the “Case Classes” lesson free?

Yes — the full text of “Case Classes” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Case Classes”?

Immutable data carriers. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Case Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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