October 2018
Intermediate to advanced
370 pages
9h 15m
English
The best approach is to use a constructor that accepts initial values at the time of object-creation. This constructor is called a primary constructor of a class. The following class declaration is an example of a primary constructor that has three properties. This single line not only declares the class, it also declares the primary constructor of the class:
class Person (val name: String, var age: Int, var height : Double)
We can write primary constructors using the constructor keyword, as shown here:
class Person constructor(val name: String, var age: Int, var height : Double)
This is not mandatory, however; constructors can be declared without the constructor keyword as well.
Read now
Unlock full access