Hanbit the Developer

Kotlin Documentation | Nested and inner classes 본문

Mobile/Kotlin

Kotlin Documentation | Nested and inner classes

hanbikan 2023. 5. 23. 15:15

Kotlin Documentation 시리즈에 대해

Category: Concepts - Classes and objects

문서 링크: https://kotlinlang.org/docs/nested-classes.html


Nested class

논리적으로 연관된 클래스를 그루핑하는 데 쓰인다.

class Outer {

    private val outer = "Outer"

    class InnerClass {

        init {
            print(outer) // ERROR
        }
    }
}

val demo = Outer.InnerClass()

자바의 static class InnerClass 와 같다.

Inner class

outer class의 멤버에 접근하여 outer class와 상호작용할 수 있는 클래스이다.

class Process {

    private val text = "text"

    inner class Thread {
    
        private val stack = "stack"

        init {
            print(text)
        }
    }
}

val demo = Process().Thread()

자바의 class InnerClass와 같다.

References

https://thdev.tech/kotlin/2020/11/17/kotlin_effective_11/