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

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…

Contents