| Free tutorials for Java, Eclipse and Web programming |
Scala provides case clases which have certain special properties compared with standard classes. Case classes support
Pattern matching
a factory method to created new instances of these classes without using the new keyword
the constructor arguments can be accessed using automatically generated accessor functions
The toString, equals and hashCode methods are automatically created using the constructor arguments
For example the following defines a object Person including accessors for firstName and lastName.
package de.vogella.scala.caseclasses case class Person (firstName: String, lastName:String)
This object uses the Person class.
package de.vogella.scala.caseclasses
object Main extends Application {
val p = Person("Lars", "Vogel")
println(p.toString())
println (p.firstName)
println (p.lastName)
}