Classes in Kotlin are declared using the keyword
The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.) and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.
A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
If the primary constructor does not have any annotations or visibility modifiers, the
constructor keyword can be omitted:
The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the
During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers:
Parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:
Kotlin has a concise syntax for declaring properties and initializing them from the primary constructor:
Such declarations can also include default values of the class properties:
You can use a trailing comma when you declare class properties:
Much the same way as regular properties, the properties declared in the primary constructor can be mutable (
var) or read-only (
If the constructor has annotations or visibility modifiers, the
constructor keyword is required, and the modifiers go before it:
Learn more about visibility modifiers.
The class can also declare secondary constructors, which are prefixed with
If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the
Code in initializer blocks effectively becomes part of the primary constructor. Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks and property initializers is executed before the secondary constructor body.
Even if the class has no primary constructor, the delegation still happens implicitly, and the initializer blocks are still executed:
If a non-abstract class does not declare any constructors (primary or secondary), it will have a generated primary constructor with no arguments. The visibility of the constructor will be public.
If you don't want your class to have a public constructor, declare an empty primary constructor with non-default visibility:
Creating instances of classes
To create an instance of a class, call the constructor as if it were a regular function:
Creating instances of nested, inner and anonymous inner classes is described in Nested classes.
Classes can contain:
Classes can be derived from each other and form inheritance hierarchies. Learn more about inheritance in Kotlin.
A class and some of its members may be declared
abstract. An abstract member does not have an implementation in its class. You don't need to annotate an abstract class or function with
You can override a non-abstract
open member with an abstract one.
If you need to write a function that can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class.
Even more specifically, if you declare a companion object inside your class, you can access its members using only the class name as a qualifier.