Sunday, February 1, 2015
Scala supports an interesting construct called case classes with the intent of creating immutable objects that can be used in pattern matching. This article provides a quick introduction to Scala case classes with examples.

A case class is created by adding the keyword case before the class definition.

case class Employee

Scala Case Classes can be used to automatically generate boilerplate code in classes that are primarily intended for storing data. Following methods are automatically generated when a class is defined as case class.
  1. apply() method is generated so that there is not need to use "new" keyword for creating a new case class instance.
  2. Getters are automatically defined.
    • Constructor parameters are considered val by default and hence only getters (accessors) are generated. If constructor parameters are declared var setters (mutators) are also automatically generated.
  3. toString() method is generated to simplify logging and debugging.
  4. equals() and hashCode() methods are generated which is used for instance comparison.
  5. unapply() method is generated which extracts/ decomposes the instance into tuples/ parts which enables using case classes in pattern matching.
  6. copy() method is generated which enables cloning an object.
Let us look at a simple case class example to understand the usage of generated methods. By just using the keyword case in class declaration Scala automatically generates all the required boilerplate code.

case class Employee (id: Int, name: String, dept: Int) {

object EmployeeTest {
  def main(args:Array[String]) {
    // "new" keyword is not used as apply() method is generated
    val emp1 = Employee(1, "John", 100);
    // Getter methods for constructor parameters
    println( + " " + + " "  + emp1.dept)
    // Clone the object using copy method
    var emp2 = emp1.copy(2, "Smith")
    // Use toString()
    // Uses equals() and hasCode() for comparison
    if ( emp1 == emp2 )
      println("Objects are same")
      println("Objects are different")

    // Extract information using unapply() method
    emp1 match { case Employee(id, name, dept) => println(id + " " + name + " " + dept) }

This example produces the following output.

1 John 100
Objects are different
1 John 100

Proceed to read other Scala Tutorials.


  1. great doing. and i happy to visit your blog to know about this wonderful information. thanks for sharing. keep updating.
    ccna Training in Chennai

  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care.As always, we appreciate your confidence and trust in us..SAP Training in chennai

  3. This topic has always been one of my favorite subjects to read about. I have found your post to be very rousing and full of good information. I will check your other articles shortly.
    car service alexandria va

  4. These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post.
    Limo dc

  5. You need to take part in a contest for one of the finest websites online. I’m going to recommend this web site! Medical manager billing software

  6. Awesome information. I am really surprised with this topic. Keep up the good work and post more here to read. Party bus limo hire in Baltimore

  7. Well written post, in this post you are moving very important issues.
    see more


Contact Form


Email *

Message *

Back to Top