Kotlin Classes

Basic Usage


class keyword is used to define the class
It is consisted of: class name, class header (type parameters, constructor) and class body (curly braces)

class Person { ... }


There are multiple ways to define class constructor.
Primary constructor is part of the class header (name field with data type):

class Person(name: String) { ... }

Secondary constructor: using constructor keyword inside class body

class Person { constructor(parent: Person) { parent.children.add(this) } }


Class instance can be created as regular function
There is no need for new keyword

val john = Person("John Wayne")


This article needs improvement. You can help improve this article. You can also write similar articles and help the community.