Data classes

Data classes are the right solution when we want to hold and transfer data between system layers. Like in Scala, these classes offer some built-in functionalities such as getters/setters, equals and hashCode, toString method and the copy function.

Let's create an example for that:

data class Book(val author:String,val name:String,val description:String,val new:Boolean = false)

We have some interesting things in the code. The first thing we notice is that all of the attributes are immutable. It means there are no setters for all of them. The second is that in the class declaration, we can see a list of attributes. In this case, Kotlin will create a constructor with all attributes present in this class and because they are val ...

Get Spring 5.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.