0Pricing
Android Academy · Lesson

Classes and Properties

Model data with classes.

Classes and Properties is a free Android Academy lesson on CoddyKit — lesson 2 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 Class?

A class is a blueprint for creating objects. It groups related data and behavior into one type.

For example, a Dog class could describe every dog's name, age, and the bark it makes.

Declaring a Class

You declare a class with the class keyword and a name. Class names use PascalCase.

An empty class is perfectly valid; you can even drop the braces.

class Dog

fun main() {
    println("Class declared")
}

Creating an Object

An object (or instance) is a real thing built from a class. In Kotlin you create one by calling the class name like a function.

Notice there is no new keyword.

class Dog

fun main() {
    val rex = Dog()
    println(rex)
}

Properties

A property is a variable that belongs to a class. Declare it inside the class body with val or var.

Use val for values that never change, and var for values that can.

class Dog {
    var name: String = "Rex"
    var age: Int = 3
}

fun main() {
    val d = Dog()
    println(d.name)
}

Accessing Properties

Use dot notation to read or change a property: object.property.

Because name is a var, we can assign a new value to it.

class Dog {
    var name: String = "Rex"
}

fun main() {
    val d = Dog()
    d.name = "Buddy"
    println(d.name)
}

val vs var Properties

A val property is read-only after it is set. Trying to reassign it causes a compile error.

A var property can be reassigned as many times as you like.

class Circle {
    val pi = 3.14
    var radius = 1.0
}

fun main() {
    val c = Circle()
    c.radius = 5.0
    println(c.radius)
}

Methods

A function declared inside a class is called a method. Methods can use the class's properties directly.

Here bark uses the name property.

class Dog {
    var name: String = "Rex"
    fun bark() {
        println(name + " says Woof!")
    }
}

fun main() {
    Dog().bark()
}

Multiple Objects

Each object has its own copy of the properties. Changing one object does not affect another.

Here a and b keep separate names.

class Dog {
    var name: String = ""
}

fun main() {
    val a = Dog()
    val b = Dog()
    a.name = "Rex"
    b.name = "Max"
    println(a.name + " " + b.name)
}

Default Property Values

Every property needs a value. You can set a sensible default when you declare it.

This way a new object starts in a known state instead of being undefined.

class Counter {
    var count: Int = 0
    fun increment() {
        count = count + 1
    }
}

fun main() {
    val c = Counter()
    c.increment()
    println(c.count)
}

Combining Properties and Methods

A class is most useful when properties and methods work together. A method can read and update the object's own properties.

Here haveBirthday increases the age property each time it is called.

class Dog {
    var name: String = "Rex"
    var age: Int = 1
    fun haveBirthday() {
        age = age + 1
    }
}

fun main() {
    val d = Dog()
    d.haveBirthday()
    println(d.age)
}

Classes Model Real Things

Classes let you model real-world ideas in code.

  • Properties describe what something has.
  • Methods describe what it can do.

In Android, screens, buttons, and view models are all built from classes.

Quick Check

Test your understanding of classes and properties.

Recap

You learned that a class is a blueprint and an object is a built instance.

Properties store data with val or var, methods add behavior, and dot notation accesses them. Next you will set up properties cleanly using constructors.

Frequently asked questions

Is the “Classes and Properties” lesson free?

Yes — the full text of “Classes and Properties” 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 “Classes and Properties”?

Model data with classes. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Classes and Properties” 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