Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Constructor

In the previous lesson, you saw how the class is defined with properties to be set during instantiation. Now go back to the same example and look at it with a magnifying glass:

class Food(var name: String, var price: String)
class Food constructor(var name: String, var price: String)
class Food private constructor(var name: String, var price: String)

Secondary Constructor

Here’s a question: Can you have more than one constructor? Yes, that’s actually the primary purpose of the constructor keyword since you don’t see it in the class header. You can have as many constructors as you need, but try to be reasonable! Secondary constructors are used to provide additional ways to create an instance of the class with different data sets. For example when class takes measurements in inches and centimeters, you can have two constructors, one for each unit of measurement, but there’s little value of two constructors that take measurements in meters and kilometers.

class Food {
  val name: String
  var price: String = "1.0"
  var origin: String = "US"

  constructor(name: String) {
    this.name = name
  }
}
class Food {
class Food() {
* Property must be initialized or be abstract
* Primary constructor call expected
//1
class Food(val name: String) {
  var price: String = "1.0"
  var origin: String = "US"

  //2
  constructor(name: String, price: String) : this(name) {
    this.price = price
  }
}
  constructor(name: String, price: String) : this(name) {
    println("Secondary constructor used")
    this.price = price
  }
fun main() {
  // one-argument secondary constructor
  var tomato = Food("Tomato")
  println(tomato.name) // Tomato
  println(tomato.price) // 1.0
  println(tomato.origin) // US

  // two-argument secondary constructor
  val tomato2 = Food("Tomato", "2.0") // Secondary constructor used
  println(tomato2.name) // Tomato
  println(tomato2.price) // 2.0
  println(tomato2.origin) // US
}

Initializer Block

What if you need to execute some code during class initialization, for example, initialize property using some complex logic, or check or validate constructor parameters? Apart from the primary constructor, which can be used only to set properties but not code, there’s another language construct you can use. Initializer blocks are curly-braced blocks inside the class body and are declared with the init keyword. The code inside that block will execute during initialization, too.

  init {
    println("Init block in action")
    origin = "UK"
  }
var price: String = "1.0".also {
  println("Setting the price to $it")
}
  // init block
  tomato = Food("Tomato", "2.0")
  println(tomato.name) // Tomato
  println(tomato.price) // 2.0
  println(tomato.origin) // UK

Setting the price to 1.0
Init block in action
Secondary constructor used

Objects

We tried to avoid using the term object for class instances because the object has a special meaning in Kotlin. object is a reserved keyword used for object expressions and object declarations.

object Box {
  var size: String = "0"
  val type = "Cardboard"
}
Box.size = "3"
println(Box.size) // 3
println(Box.type) // Cardboard
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo