This page is no longer maintained — Please continue to the home page at

How does a case class differ from a normal class?

  1. You can do pattern matching on it,
  2. You can construct instances of these classes without using the new keyword,
  3. All constructor arguments are accessible from outside using automatically generated accessor functions,
  4. The toString method is automatically redefined to print the name of the case class and all its arguments,
  5. The equals method is automatically redefined to compare two instances of the same case class structurally rather than by identity.
  6. The hashCode method is automatically redefined to use the hashCodes of constructor arguments.

Most of the time you declare a class as a case class because of point 1, i.e. to be able to do pattern matching on its instances. But of course you can also do it because of one of the other points. The code example below makes use of two of the characteristics of case classes:

  1. Instances are created without resorting to the new keyword (point 2), for example in
    List(MyInt(1), MyInt(2), MyInt(3))
  2. Constructor arguments are accessed from "outside" using the automatically generated accessor functions (point 3), in
    else m.asInstanceOf[MyInt].x == x;


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland