Table of Contents
Overview
In Kotlin, object oriented programming is centered around classes and objects. A class is a blueprint that describes what something is and what it can do. An object is a real instance created from that blueprint. In Android apps you constantly work with classes, such as Activity, View, and Intent, and you create objects from these classes to make your app do real work.
In this chapter you will learn how to declare your own classes, create objects from them, define properties and functions inside them, and understand how Kotlin handles these concepts in a concise way.
Defining a Simple Class
A basic class in Kotlin is declared with the class keyword followed by a name. By default, such a class has no properties and no behavior until you add them.
class User {
}
This example defines a class named User. It is just an empty blueprint. To make it useful, you add properties and functions inside its body.
Kotlin lets you omit the curly braces when the class has no body.
class Empty
You can put classes in their own .kt files, often with the same name as the class. For example, a class named User is typically defined in a file named User.kt. This keeps your project organized and easier to navigate, especially as an Android app grows.
Properties Inside a Class
Properties are variables that belong to a class. Each object created from that class has its own copy of these properties, which together represent the state of that object.
Inside a class, you declare properties using val for read only values or var for mutable values.
class User {
var name: String = ""
var age: Int = 0
val isAdult: Boolean = false
}
Here each User object has a name, an age, and an isAdult flag. When you create multiple User objects, each one can have different values.
Kotlin lets you write property declarations in a compact way and initializes them directly inside the class. You will learn more about initialization when working with constructors, but at this stage it is useful to see that properties are associated with each object, not the class itself.
Creating Objects
Once you have a class, you can create objects from it. Creating an object is also called instantiating a class.
By default, if a class has a simple primary constructor with no parameters, you can create an object using the class name followed by parentheses.
val user = User()
Here user is an object of type User. The type can be written explicitly.
val user: User = User()
Each new call to User() creates a different object with its own properties. So if you create two users, they do not share their values.
val user1 = User()
val user2 = User()
user1.name = "Alice"
user2.name = "Bob"
In this example user1.name is "Alice" and user2.name is "Bob", because each object has its own state.
Accessing Properties and Functions
You use the dot operator to work with the members of an object. A member can be a property or a function defined in the class.
For properties, you read and write values like this:
user.name = "Charlie"
println(user.name)
This is similar to how you will work with Android classes. For example, calling textView.text = "Hello" or accessing properties in a Button uses the same idea.
You can think of the dot operator as a way to say “use this thing that belongs to that object.”
Methods: Functions Inside Classes
Functions defined inside a class are often called methods. They describe what objects of that class can do. You define them similarly to normal functions, but they are placed inside the class body.
class User {
var name: String = ""
var age: Int = 0
fun printInfo() {
println("Name: $name, age: $age")
}
fun birthday() {
age = age + 1
}
}
Here printInfo and birthday are methods of the User class. You call them on an object with the dot operator.
val user = User()
user.name = "Dana"
user.age = 20
user.printInfo() // prints: Name: Dana, age: 20
user.birthday()
user.printInfo() // prints: Name: Dana, age: 21
Inside these methods, you can directly access the properties name and age. Kotlin automatically refers to the current object.
The `this` Reference
Sometimes you need to refer explicitly to the current object inside a class. Kotlin provides the keyword this for that purpose. It represents the object on which the method or property is currently operating.
In many cases you do not need to write this because Kotlin can infer it. However, it is useful when a parameter name is the same as a property name.
class User {
var name: String = ""
fun setName(name: String) {
this.name = name
}
}
Here the class has a property name and the method setName has a parameter also called name. In the body, this.name refers to the property, and name by itself refers to the parameter. This is a common pattern when you want to update properties from function arguments.
this can also help clarify code, especially in more complex classes. For example, you might write this.printInfo() to show you are calling a method on the current object.
Initializing Objects and Default Values
In the earlier examples, properties are given default values directly when declared. When you instantiate the class, each object starts with those default values. For instance, name might start as an empty string and age as zero.
Kotlin also supports more advanced ways to pass initial values when you create objects, which involves constructors. Since constructors are covered as a separate topic, here you only need to observe that simple classes can rely on default property values, and you can change them after object creation using the dot operator.
Important: Every object has its own copy of instance properties. Changing a property on one object does not change the same property on another object created from the same class.
This separation of state between objects is a core part of object oriented programming and is heavily used in Android. For example, two different Button objects can each have their own text and their own click behavior.
Instance vs Class Level Members (Preview)
So far, every property and function you have seen belongs to an individual instance of the class. These are called instance members, because they are tied to a specific object that you created.
Sometimes you need behavior or data that belongs to the class itself, not to any particular object. Kotlin supports this through companion object or top level declarations. These ideas are introduced in more detail elsewhere. At this point, it is enough to understand that there is a difference between things that exist per object and things that exist once for the whole class.
An Android example that uses this idea is a constant defined once and used by all activities, such as an extra key for an Intent.
Using Classes and Objects in Android
In Android development, you rarely work with raw Kotlin classes only for learning. Instead, you create classes that extend Android framework classes and define objects that interact with the system and the user interface.
When you define your own model classes, such as User, Product, or Message, you use the same patterns shown in this chapter. You declare properties that represent data, add methods that work with that data, and then create objects to pass around in your app, for example to display a list of users in a RecyclerView.
Understanding how to declare classes, create objects, and use properties and methods is therefore a foundation that will keep appearing in almost every Android feature you implement.