Constructors and initialization checks
Constructors and initialization checks
A mark should never begin outside zero through one hundred. A primary constructor declares the inputs for creating an instance, while init blocks establish additional rules and state. Secondary constructors must delegate under Kotlin’s construction rules.
What you’ll learn
- Validate constructor inputs.
- Avoid partially valid domain records.
Constructors and initialization checks in the example
Constructor input
A mark should never begin outside zero through one hundred.
Constructor inputRange requirement
Use require for an argument contract when failure should be an exception.
Range requirementValid instance or failure
The valid constructor completes with value 80.
Valid instance or failure
Understand constructors and initialization checks
Use require for an argument contract when failure should be an exception. Init executes during construction, and property initializers and init blocks follow their declared order. A successful type conversion does not itself establish a valid domain value.
Follow constructors and initialization checks
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
class Mark(val value: Int) {
init { require(value in 0..100) { "Score must be 0..100" } }
}
fun main() {
println(Mark(80).value)
try { Mark(120) }
catch (e: IllegalArgumentException) { println("Invalid score") }
}Expected output
80
Invalid scoreFollow the execution
The valid constructor completes with value 80. The second construction fails its requirement, and the catch reports Invalid score. No usable Mark with value 120 is returned.
Try this in the Kotlin compiler by pasting the complete program.
Practice: Constructors and initialization checks
Your task: What happens when constructing Mark(-5)?
Kotlin/JVM · complete program
class Mark(val value: Int) {
init { require(value in 0..100) { "Score must be 0..100" } }
}
fun main() {
println(Mark(80).value)
try { Mark(120) }
catch (e: IllegalArgumentException) { println("Invalid score") }
}Worked answer
The requirement fails and throws IllegalArgumentException. The caller must handle that failure if invalid input is an ordinary possibility.
Constructors and initialization checks: the important boundary
Protect later changes too: a valid initial value can become invalid if an unrestricted mutable property is exposed. Avoid calling overridable behaviour during initialization when subclass state is not ready.
Check your understanding
Constructors and initialization checks: what to keep in mind
The requirement fails and throws IllegalArgumentException. The caller must handle that failure if invalid input is an ordinary possibility. Protect later changes too: a valid initial value can become invalid if an unrestricted mutable property is exposed.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.