Constructors and init
Create and initialize objects.
Constructors and init is a free Android Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.
What Is a Constructor?
A constructor is special code that runs when you create an object. It sets up the object's starting values.
In Kotlin the main constructor is written right next to the class name.
The Primary Constructor
The primary constructor goes in parentheses after the class name. You pass values when creating the object.
Here Dog requires a name when constructed.
class Dog(name: String) {
val dogName = name
}
fun main() {
val d = Dog("Rex")
println(d.dogName)
}Properties in the Constructor
Adding val or var in front of a constructor parameter turns it into a property automatically.
This is the most common, concise Kotlin style.
class Dog(val name: String, var age: Int)
fun main() {
val d = Dog("Rex", 3)
println(d.name + " is " + d.age)
}Default Constructor Values
Constructor parameters can have default values too. Callers may then leave them out.
Here age defaults to 0 if not supplied.
class Dog(val name: String, var age: Int = 0)
fun main() {
val d = Dog("Max")
println(d.age)
}The init Block
An init block runs as part of the primary constructor. Use it for setup logic that does not fit on a single property line.
It can read the constructor parameters.
class Dog(val name: String) {
init {
println("Created dog: " + name)
}
}
fun main() {
Dog("Rex")
}Validating in init
A common use of init is checking the inputs. Here we guard against a negative age using require.
If the check fails, the object is never created.
class Person(val age: Int) {
init {
require(age >= 0) { "Age cannot be negative" }
}
}
fun main() {
val p = Person(25)
println(p.age)
}Order of Execution
Property initializers and init blocks run top to bottom in the order they appear in the class.
This matters when one line depends on a value set above it.
class Demo {
val a = 1
init { println("a is " + a) }
val b = a + 1
init { println("b is " + b) }
}
fun main() {
Demo()
}Computed Properties
You can compute a property from constructor values inside the class body.
Here area is derived from width and height.
class Rect(val width: Int, val height: Int) {
val area = width * height
}
fun main() {
println(Rect(3, 4).area)
}Secondary Constructors
A class may also have secondary constructors using the constructor keyword. They must delegate to the primary one with this(...).
They are less common because default values usually suffice.
class Dog(val name: String) {
constructor() : this("Unknown")
}
fun main() {
println(Dog().name)
}Using the New Object
Once the constructor and init have run, the object is ready to use. You can read its properties and call its methods immediately.
Here the constructor sets up a balance, and a method spends from it.
class Account(var balance: Int) {
fun spend(amount: Int) {
balance = balance - amount
}
}
fun main() {
val acc = Account(100)
acc.spend(30)
println(acc.balance)
}Constructors Set the Stage
Constructors and init blocks make sure every object starts in a valid state.
- Primary constructor: declare and receive values.
init: run setup and validation.
This prevents half-built objects in your app.
Quick Check
Test your understanding of constructors and init.
Recap
You learned the primary constructor, declaring properties with val/var in it, and giving them defaults.
The init block handles setup and validation, and runs top to bottom with property initializers. Next you will meet data classes, which generate this boilerplate for you.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Constructors and init” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Constructors and init”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.
What will I learn in “Constructors and init”?
Create and initialize objects. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Constructors and init” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Functions
- Classes and Properties
- Constructors and init
- Data Classes