In Part 1 we dealt with the simplest form of initialization with the primary constructor:
class Planet( val diameter: Int, val distanceToSun: Double, val mass: Double )
But wait: there is more:
Init block
The primary constructor cannot contain any code so init block for the rescue:
class Planet( val diameter: Int, val distanceToSun: Double, val mass: Double ) { val circumference = PI * diameter init { println(circumference) } }
Notes:
- You can have more than one init block
- they run in order of their declaration