From the course: Build Spring Boot Apps with the Kotlin Programming Language
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Data classes
From the course: Build Spring Boot Apps with the Kotlin Programming Language
Data classes
- [Instructor] Now, that is almost the basics of classes covered. Although we've managed to remove a lot of Java's boilerplate code by creating the classes in this way, we still have had to write the toString method. We probably should have done a hashcode and an equals. So there's still some boilerplate required. I think, in fact, let's work now on that top version up here. This class, this Customer class up here. So here we've got constructors, we've got the equivalents of gets and sets created for it. But if I was going to do a good job on this Customer class, I'd still want to create a hashcode and an equals. So because of that, Kotlin gives us an alternative way to create a class which is useful specifically when the class is designed to store data. And that is, we put the keyword data in front of the keyword class. So I'm going to change Customer from a class to a data class. That's the only change I need to make. And the difference, by putting that keyword data in, is it now…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Project structure and packages2m 5s
-
(Locked)
The class declaration and class-level attributes3m 31s
-
Instantiating a class and accessing class-level attributes2m 14s
-
(Locked)
Creating a simple constructor5m 7s
-
(Locked)
Secondary constructors5m 20s
-
(Locked)
Alternative class design5m 3s
-
(Locked)
Overriding getters and setters7m 21s
-
(Locked)
Functions within a class2m 57s
-
(Locked)
Static functions3m 28s
-
(Locked)
Data classes7m 32s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-
-
-