Updated April 4, 2023
Introduction to Scala Case Class
Case classes in scala can be defined by using the case keyword. This class is a normal class like any other class in scala, but these classes are immutable, meaning we cannot change their value once assigned. Also, case classes in scala do not require a new keyword to create an object of the class; they have a default method called apply(), which takes care of object creation. Case class in scala is used in pattern matching.
Syntax
While defining a case class, we just require a case keyword followed by the name of our class and a list of parameters; if any, otherwise, it can be empty as well. Also, we can have one practice example of the syntax for beginners to understand.
Case class class_name_here(list_of_parameter)
Example:
Case class Demo(name:String)
Here we have defined a case class with a name demo and passing one parameter inside the bracket. This parameter list can also be empty if not required; it totally depends upon the requirement.
How does Case Classwork in Scala?
Case class in scala uses apply method, which manages the creation of objects; we do not need to use a new keyword while using the case class. Also, for the comparison of the object, this class uses the equals method, and this class is immutable in nature; also, the parameter that we used inside this class is by default public. They are mainly used with pattern matching in scala.
Now let’s have a look at creating the object for the case class;
Example:
case class Demo(id: Int)
val demo_var = Demo(100)
In the above lines of code, we have one case class defined as Demo, and in the next line, we are trying to use the case class by creating its object and assigning a value for the variable. But we have not used a new keyword here to instantiate it. So now, let’s see one syntax to use the parameter of the case class in scala.
Example:
case class Demo(id: Int, name: String, age: Int)
val case_var = Demo(200, "test case", "30")
// now try to print the parameter.
println(case_var.name)
The above lines of code will work and print the corresponding name in the console as output. But if we try to reassign the parameter of the case class, then it is not possible also; all the parameters are public by default. Let’s try to reassign it; see below;
Example:
case_var.name = "this will not work."
Also, case class objects are compared based on the object’s structure, not by the reference like in java. If the structure of the objects is the same, then they are considered equal. Let’s have a look at how we can define them;
Example:
case class Demo(id: Int, name: String, age: Int)
val case_var1 = Demo(200, "test case", "30")
val case_var2 = Demo(200, "test case", "30")
val result = case_var1 == case_var2
In the above lines of code, we have created two case objects. Both are the same in structure as well as in value. So in the case of class in scala, it will compare the structure of the object rather than its reference. So the output for the above lines of code will be true. Also, we can create a copy of the object for this it provides us with a copy() method, but it will create one shallow copy of the instance. So let’s see one example of how we can achieve it in code.
Example:
case class Demo(id: Int, name: String, age: Int)
val case_var1 = Demo(200, "test case", "30")
val case_var2 = case_var1.copy(id = case_var1.id, name = case_var1.name, age = case_var1.age)
// print the values
printn(case_var2)
In this example, we are creating two objects, but the second object is the copy of the first object. Therefore, we are using the copy() method to create a shallow copy of the case object.
We also have a case object when and these terms are used interchangeably often. Let’s discuss some important points related to class/ object, which are mentioned below;
- The class which does not contain any parameter into it then that case class is referred to as an object.
- Scala case objects are the same as the other objects in scala, but they are the combination of case class and case object.
- Case object contains more features and attributes than other objects in the scala.
- Case objects are serializable, and they have by default implementation for the hashcode method in it.
Examples of Scala Case
Different examples are mentioned below:
Example #1
A simple example for beginners to see how to deal with case class and print the details.
Code:
case class Student (id:Int, name:String, age:Int, city: String)
object Main
{
def main(args: Array[String])
{
var s1 = Student(001, "Amit verma", 20, "Mumbai")
var s2 = Student(002, "Sumit sharma", 15, "Delhi")
var s3 = Student(003, "Vihit verma", 18, "Pune")
var s4 = Student(004, "Nimit shah", 20, "Bihar")
println("detail of first student ::")
println( "is di :: " + s1.id + " name is :: " + s1.name + " age is :: " + s1.age + " city is :: " + s1.city)
println("detail of second student ::")
println("is di :: " + s2.id + " name is :: " + s2.name + " age is :: " + s2.age + " city is :: " + s2.city)
println("detail of third student ::")
println("is di :: " + s3.id + " name is :: " + s3.name + " age is :: " + s3.age + " city is :: " + s3.city)
println("detail of fourth student ::")
println("is di :: " + s4.id + " name is :: " + s4.name + " age is :: " + s4.age + " city is :: " + s4.city)
}
}
Output:
Example #2
Checking if the objects defined are equal or not in the case.
Code:
case class Student (id:Int, name:String, age:Int, city: String)
object Main
{
def main(args: Array[String])
{
var s1 = Student(001, "Amit verma", 20, "Mumbai")
var s2 = Student(002, "sumit sharma", 15, "Delhi")
var s3 = Student(003, "vihit verma", 18, "Pune")
var s4 = Student(004, "nimit shah", 20, "Bihar")
var s5 = Student(001, "Amit verma", 20, "Mumbai")
var result = s1 == s5
println("Result if they are equal or not ::")
println(result)
}
}
Output:
Conclusion
Scala case classes are other normal classes in scala. We also have a case object with us. These classes are immutable and do not compare the object’s reference when it comes to checking object is equal or not.
Recommended Articles
This is a guide to Scala Case. Here we discuss How does Case Classwork in Scala and Examples, along with the codes and outputs. You may also have a look at the following articles to learn more –