A Case Class is just like a regular class, which has a feature for modeling unchangeable data. It is also constructive in pattern matching. It has been defined with a modifier case, due to this case keyword, we can get some benefits to stop oneself from doing a sections of codes that have to be included in many places with little or no alteration. As we can see below a minimal case class needs the keyword case class, an identifier, and a parameter list which may be vacant.
Syntax:
Case class className(parameters)
Note: The Case class has a default apply() method which manages the construction of object.
A Case Object is also like an object, which has more attributes than a regular Object. It is a blend of both case classes and object. A case object has some more features than a regular object.
Below two are important features of case object:
- It is serializable.
- It has a by default hashCode implementation.
Example :
// Scala program of case class and case Object case class employee (name:String, age:Int) object Main { // Main method def main(args: Array[String]) { var c = employee("Nidhi", 23) // Display both Parameter println("Name of the employee is " + c.name); println("Age of the employee is " + c.age); } } |
Name of the employee is Nidhi Age of the employee is 23
- The one of the topmost benefit of Case Class is that Scala Compiler affix a method with the name of the class having identical number of parameters as defined in the class definition, because of that you can create objects of the Case Class even in the absence of the keyword new.
Example:// Scala program of case class and case Object// affix a method with the name of the classcaseclassBook (name:String, author:String)objectMain{// Main methoddefmain(args:Array[String]){varBook1=Book("Data Structure and Algorithm","cormen")varBook2=Book("Computer Networking","Tanenbaum")// Display stringsprintln("Name of the Book1 is "+ Book1.name);println("Author of the Book1 is "+ Book1.author);println("Name of the Book2 is "+ Book2.name);println("Author of the Book2 is "+ Book2.author);}}chevron_rightfilter_noneOutput:Name of the Book1 is Data Structure and Algorithm Author of the Book1 is cormen Name of the Book2 is Computer Networking Author of the Book2 is Tanenbaum
- The second convenience is that by default Scala compiler affixes val or var for all the parameters of constructor so, we won’t be able to reassign a new value to them once that class object is constructed thatâs why even in absence of val or var, case classâs constructor parameters will turn out to be class members, that is not practicable for regular classes.
Example:

Thus, reassignment is not feasible. - The Scala compiler also appends a copy() method to case class that is utilized to create a duplicate of the same object with changing some parameters or without altering them.
Example : To create a duplicate of same instance without altering the parameters.// Scala program of case class To create// a duplicate of same instancecaseclassStudent (name:String, age:Int)objectMain{// Main methoddefmain(args:Array[String]){vals1=Student("Nidhi",23)// Display parameterprintln("Name is "+ s1.name);println("Age is "+ s1.age);vals2=s1.copy()// Display copied dataprintln("Copy Name "+ s2.name);println("Copy Age "+ s2.age);}}chevron_rightfilter_noneOutput:Name is Nidhi Age is 23 Copy Name Nidhi Copy Age 23
Here, we have created new object s2 by using copy method on s1 object without altering s1 object attributes.
Example : To create a duplicate of same object with changing attributes.// Scala program of case class same object// with changing attributescaseclassStudent (name:String, age:Int)objectMain{// Main methoddefmain(args:Array[String]){vals1=Student("Nidhi",23)// Display parameterprintln("Name is "+ s1.name);println("Age is "+ s1.age);vals2=s1.copy(age=24)// Display copied and changed attributesprintln("Copy Name is "+ s2.name);println("Change Age is "+ s2.age);}}chevron_rightfilter_noneOutput:Name is Nidhi Age is 23 Copy Name is Nidhi Change Age is 24
Here, you need to pass the value in the copy method.
- By default Scala Compiler adds toString, equals methods, companion object with apply and unapply methods, for that reason you donât need new keyword to construct object of a Case class.
Note:It was not practicable to use more than 22 parameters in scala case classes but now you can use any number of parameters in case classes using Scala Version 2.11.1. This limitation was eliminated from this version of scala.
Recommended Posts:
- Class and Object in Scala
- Determine the class of a Scala object
- HTML | DOM Object Object
- How to check a JavaScript Object is a DOM Object ?
- How to select first object in object in AngularJS?
- How to Cast a JSON Object Inside of TypeScript Class ?
- Object Casting in Scala
- Object Equality in Scala
- Scala | Stateful Object
- How to override the CSS properties of a class using another CSS class ?
- Inner class in Scala
- Extending a Class in Scala
- Calling A Super Class Constructor in Scala
- Call a method on a Super Class in Scala
- Scala Tutorial â Learn Scala with Step By Step Guide
- How to use a switch case 'or' in PHP?
- case command in Linux with examples
- Convert string to title case in JavaScript
- Case insensitive search in JavaScript
- Switch case in R
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

