The website uses cookies to optimize your user experience. Using this website grants us the permission to collect certain information essential to the provision of our services to you, but you may change the cookie settings within your browser any time you wish. Learn more
I agree
Text direction?

Nested and Inner Classes

Classes can be nested in other classes:

class Outer {
    private val bar: Int = 1
    class Nested {
        fun foo() = 2

val demo = Outer.Nested().foo() // == 2

Inner classes

A class may be marked as inner to be able to access members of outer class. Inner classes carry a reference to an object of an outer class:

class Outer {
    private val bar: Int = 1
    inner class Inner {
        fun foo() = bar

val demo = Outer().Inner().foo() // == 1

See Qualified this expressions to learn about disambiguation of this in inner classes.

Anonymous inner classes

Anonymous inner class instances are created using an object expression:

window.addMouseListener(object: MouseAdapter() {

    override fun mouseClicked(e: MouseEvent) { ... }
    override fun mouseEntered(e: MouseEvent) { ... }

If the object is an instance of a functional Java interface (i.e. a Java interface with a single abstract method), you can create it using a lambda expression prefixed with the type of the interface:

val listener = ActionListener { println("clicked") }
Related Notes
Get a free MyMarkup account to save this article and view it later on any device.
Create account

End User License Agreement

Summary | 1 Annotations
and Inn
2017/09/18 02:09