0Pricing
Android Academy · Lesson

Constructors and init

Create and initialize objects.

Constructors and init is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Constructors and init” lesson free?

Yes — the full text of “Constructors and init” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Declaring Functions
  2. Classes and Properties
  3. Constructors and init
  4. Data Classes
← Back to Android Academy