BEST SITE FOR WEB DEVELOPERS
Kotlin. W3Schools in English. Lessons for beginners

Ua Es De

Kotlin Constructors


Kotlin Constructor

In the previous chapter, we created an object of a class, and specified the properties inside the class, like this:

Example

class Car {
  var brand = ""
  var model = ""
  var year = 0
}

fun main() {
  val c1 = Car()
  c1.brand = "Ford"
  c1.model = "Mustang"
  c1.year = 1969
}
Try it Yourself »

In Kotlin, there's a faster way of doing this, by using a constructor.

A constructor is like a special function, and it is defined by using two parantheses () after the class name. You can specify the properties inside the parantheses (like passing parameters into a regular function).

The constructor will initialize the properties when you create an object of a class. Remember to specify the type of the property/variable:

Example

class Car(var brand: String, var model: String, var year: Int)

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
}
Try it Yourself »

Now it's even easier to specify multiple objects of one class:

Example

class Car(var brand: String, var model: String, var year: Int)

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  val c2 = Car("BMW", "X5", 1999)
  val c3 = Car("Tesla", "Model S", 2020)
}
Try it Yourself »