1 Class every Android Developer should be aware of: Data Classes
When I first started as an Android developer, I realized that my skills weren’t where they needed to be. So, I made a decision: I would improve by learning the foundations of Kotlin. One of the key concepts I’ve come to understand is Data Classes. If you’re an Android developer, chances are you’ve encountered data classes, especially when working with models to handle API responses. Let me share what I’ve learned about them.
What Are Data Classes?
Data classes in Kotlin are used primarily to hold or store data. They’re particularly handy in situations where you need to manage and pass around data within your application, like receiving data from an API call and storing it in a model. These models then carry the data throughout your app, allowing you to manipulate and display it as needed.
Creating a Data Class
A data class in Kotlin is easy to recognize — it begins with the keyword data before the class declaration. Here’s a simple example:
data class User(val name: String, val age: Int)
In this example, the User is a data class that holds two properties: name and age. Notice a few things:
- The use of val or var: In a data class, each property must be defined with either val (immutable) or var (mutable). This isn’t mandatory in regular classes but is crucial for data classes because it tells Kotlin how to handle the data.
- Class Naming: Just like with any other class, the name of a data class should start with a capital letter to follow Kotlin’s naming conventions.
- Restrictions: Data classes in Kotlin have some limitations. They cannot be abstract, open, sealed, or inner classes. These restrictions ensure that data classes remain focused on their primary purpose — storing data.
Special Properties of Data Classes
One of the powerful aspects of data classes is the set of automatically generated functions that make them so useful:
- toString(): This function outputs the contents of the data class in a readable format, which is incredibly useful for debugging. For example:
val user = User("Hitesh", 28)
println(user.toString())
// Output: User(name=Hitesh, age=28)
- equals(): This function compares two instances of a data class based on their values rather than their references. So if two objects have the same data, they’re considered equal.
val user1 = User("Hitesh", 28)
val user2 = User("Hitesh", 28)
println(user1 == user2)
// Output: true
- copy(): This function allows you to create a copy of an existing object, with the option to change some of its properties.
val user1 = User("Hitesh", 28)
val user2 = user1.copy(age = 30)
println(user2)
// Output: User(name=Hitesh, age=30)
These functions are what make data classes stand out from regular classes, which don’t automatically provide such functionality.
Referential Equality in Kotlin
Sometimes, you might need to check whether two variables point to the same object in memory, which is known as referential equality. In Kotlin, you can do this using ===.
For example:
val user1 = User("Hitesh", 28)
val user2 = user1
println(user1 === user2)
// Output: true
In this case, user1 and user2 are referencing the same object, so === returns true.
Additional Functionality in Data Classes
One more thing to note is that data classes aren’t limited to just holding data. You can define functions inside a data class, just like you would in any other class. This allows you to encapsulate behaviour that’s closely related to the data.
data class User(val name: String, val age: Int) {
fun greet() = "Hello, my name is $name and I am $age years old."
}
Now, calling user.greet() will return a string introducing the user, which can be particularly useful for keeping your code organized and clean.
If you want to learn more about Data classes. Here is a video for you:
Final Thoughts
Understanding data classes is a crucial part of mastering Kotlin and becoming a better Android developer. They’re more than just containers for data — they provide built-in functionality that can save you time and effort. By mastering these foundational elements, you’ll be well on your way to writing cleaner, more efficient code.
I hope this explanation has made Kotlin’s data classes clearer. We’ll continue exploring more concepts in the upcoming blogs. If I’ve made any errors, please forgive me. Thanks for reading!
Your Captain (the Commute),
Hitesh Kohli