Define A Constructor
A Constructor is used to specify how the objects of a class are created. They initialize an object, making it ready for use.
The code in the constructor executes when the class object is instantiated.
How then do we define a constructor?
With parameters
Without parameters
These are implemented as follows:
Default Constructor
We define this type of constructor without parameters as follows:
class SoftwareEngineer constructor() {
...
}
To note:
You can remove the parenthesis if there are no parameters to the constructor.
You can remove the constructor keyword if there are no annotations or visible modifiers. This is influenced by Kotlin’s aim to be concise.
The Kotlin compiler autogenerates the default constructor in the background meaning your code will not show you the default constructor, i.e.
class SoftwareEngineer {
...
}
Define A Parameterized Constructor
A parameterized constructor is used to maintain the immutability of class properties while avoiding hardcoded values. For instance, a SoftwareEngineer
object should have a name(name).
To ensure all instances of the SoftwareEngineer()
initialize this, we define the constructor as follows:
// class [ClassName](Optional constructor arguments)
class SoftwareEngineer(val name: String) {
var employed: Boolean = false
var student: Boolean = false
var experience: Double = 0.0
fun gotAJob() {
// we'll use this to update the employed variable
employed = true
println("Excited to start a new role!")
}
fun leftAJob(){
// we'll use this to update the employed variable
employed = false
println("Sad to have lost the role!")
}
}
Please Note:
You cannot use mutable variables in parameterized constructors for two reasons:
Properties vs. Parameters: parameters within the primary constructor are not directly accessible as variables. They are immediately converted to class properties. When using
val
orvar
before a parameter name, you’re defining a property that gets its initial value from the constructor parameter.Immutability of Const Values: Since constructors with
val
orvar
parameters define properties, they cannot beconst val
. Kotlin’sconst val
ensures values cannot be changed after initialization, and mutable variables inherently allow changes.
Types Of Constructors
Primary Constructor: a class can only have one, which is defined as part of the class header. The primary constructor can be a default or parameterized constructor. This doesn’t have a body and thus doesn’t contain code.
// Primary Constructor Implementation class SoftwareEngineer constructor (val name: String) { // body }
Secondary Constructor: can be multiple in a class. The secondary constructor can be defined with or without parameters. This can initialize the class and has a body containing initialization logic. If the class has a primary constructor, each secondary constructor needs to initialize the primary constructor.
The secondary constructor is enclosed within the body of the class, and contains three parts:
Secondary constructor declaration: the
constructor
keyword followed by parentheses.Primary constructor initialization: a colon followed by
this
keyword and a set of parentheses.Secondary constructor body: initialization of the primary constructor is followed by a set of curly braces, containing the body.
// Primary Constructor
class SoftwareEngineer constructor(val name: String) {
...
// Secondary Constructor
constructor(name: String, experience: Int, employed: Boolean) : this (name) {
// initialization logic
}
...
}
Conclusion
Constructors are cool but need to be well understood, so read that again if you need a better understanding of things.
Asalaam Aleykum!