What a class is
Kotlin comes with types such as Int, String and Array. A class lets you add your own: a Dog, a BankAccount, a Game. A class is a blueprint. It describes what data a thing holds and what it can do, and later lessons make real objects from it.
Without classes, a program about dogs would need one variable per dog name, another per age, and loose functions for every action. A class bundles all of that under a single name.
The syntax
The smallest class is a keyword, a name and a pair of braces:
class Dog {
}
classstarts a class declaration.Dogis the name you choose. By convention Kotlin class names use PascalCase: each word capitalized, no underscores, as inDog,BankAccountorHighScore.- The braces hold the class body. An empty body is still a valid class, and Kotlin lets you drop the braces entirely when there is nothing inside:
class Dogmeans the same thing.
What goes inside a class
An empty class is a starting point. A class body can hold properties, the data each object carries, such as a name and an age, and functions, the actions an object can perform, such as barking. It can also have constructors that set up a new object. Later lessons add each of these to Dog. For now the class is only a name for a new kind of thing, and an object made from it carries no data and has no behavior of its own.
Where the class goes
A class can be declared at the top level of a file, next to other classes and functions, which is where the lesson puts Dog. Kotlin does not require a class to appear before the code that uses it, so Dog could equally sit below Solution. Placing it above is simply easier to read.
A class is also final by default: no other class can extend it unless you mark it open. That does not matter for this lesson, but it is a Kotlin habit worth knowing early.
The exercise
The starter code already has a Solution class whose mainFunction uses a Dog:
class Solution {
fun mainFunction() {
val dog = Dog()
println("Defined the Dog class!")
}
}
The line Dog() creates an instance of Dog. It works only if a class with exactly that name exists. Without it the code fails to compile with an error such as unresolved reference 'Dog', and nothing runs.
The single step is to declare the empty class above Solution. An empty class has a default constructor with no parameters, so Dog() is valid with no extra code. The finished lesson:
class Dog {
}
class Solution {
fun mainFunction() {
val dog = Dog()
println("Defined the Dog class!")
}
}
Running mainFunction prints Defined the Dog class!. The message is printed by the lesson's own println; the class is what makes the line above it compile.
Common mistakes
- Wrong capitalization. Kotlin names are case-sensitive. A class called
dogorDOGdoes not satisfyDog(), which then fails withunresolved reference. - Forgetting
class. Without the keyword,Dog { }is not a declaration, and the code fails to compile. - A missing closing brace. The compiler reaches the end of the file while still inside the body and reports an error.
- Expecting the class to do something by itself. Declaring a class prints nothing and creates no dog. Only a call such as
Dog()creates an object.
Key takeaways
- A class is a blueprint that defines your own type.
- Declare it with
class, a PascalCase name and braces; the body may be empty or omitted. - Every class gets a default no-argument constructor unless you write your own, so
Dog()works right away. - Declaration order at the top level does not matter in Kotlin, but placing the class first reads best.