-
Book Overview & Buying
-
Table Of Contents
Android Programming for Beginners - Fourth Edition
By :
Data classes are a type of class that—wait for it—are ideal for representing data. When we create a data class, we are upfront in declaring that it holds data rather than functionality (and data).
Because a data class mainly models data rather than behavior, it often has no functions at all — and that's perfectly normal. However, while we can define functions for data classes, they shouldn't change that data. For example, we might have functions that format the data or do calculations with the data, but we shouldn't have functions that change that data.
This is not to say that when we use a data class, our app's data must always be the same; rather, a common approach is to create a new instance with the changed values instead of modifying the original. This immutable style isn't a strict rule for every data class, but it's widely used — and it's exactly what we'll do in the Celeb Book app.
When we declare...