-3

Sometimes I see people using standalone case classes for general purposes, instead of pattern matching, for example,

case class Employee(id: Int, name: String, age: Int, city: String)

What's the advantage using case classes like this over normal classes?

class Employee(id: Int, name: String, age: Int, city: String)
Milo Lu
  • 3,176
  • 3
  • 35
  • 46

1 Answers1

1

class Employee(id: Int, name: String, age: Int, city: String)

when you declare a class like above, every field is part of the constructor, not class members. To make them class fields, you need to add val before every field. But in the case of case class by default, they are members of the class.

Besides that, be default case class has toString, hashcode and equals method.

For more benefits blog1 blog2 stackoverflow

Mahesh Chand
  • 3,158
  • 19
  • 37