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.
Creating a simple constructor
From the course: Build Spring Boot Apps with the Kotlin Programming Language
Creating a simple constructor
- [Instructor] Now having fixed values for our parameters doesn't really make much sense. What would be more sensible is if we were setting them in a constructor, especially because at least two of these are vals. So let's change this to do that. So I'm going to remove the default values for all of these variables, and instead what I'd like to do is add into this class a constructor where we can set these values. Now the concept is exactly the same as what we do in Java, but instead of creating a method with the same name as the class type to create a constructor, we simply use the key-word, constructor, and then we're going to pass some parameters in here, and an implementation for the parameters inside curly brackets. So our constructors need to go in to take three parameters, so they can be the name, the address, and of course the age. And then within our implementation, well we'll do exactly the same as you'd expect, we do what we do in Java, that will be this.name = name and so…
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)
-
-
-
-
-
-
-
-
-
-
-
-
-
-