https://kotlinlang.org logo
Title
c

chansek

08/05/2017, 1:58 PM
If you have an inner class which should be instatiated inside the class where it is declared. But the object should be accessible outside the class through some getter. How it is possible in Kotlin? If not possible, then why it is restricted in Kotlin?
d

diesieben07

08/05/2017, 2:24 PM
Works just fine:
class Outer {

    val inner = Inner()

    inner class Inner

}

fun main(args: Array<String>) {
    val outer = Outer()
    println(outer.inner)
}
c

chansek

08/06/2017, 3:21 PM
This will also allow other classes to instantiate the inner class object like val inner = Outer().Inner() from anywhere. What I want is Inner class object should only be instantiated inside Outer class and not outside.
d

diesieben07

08/06/2017, 3:56 PM
I think the best you can do is make the
Inner
constructor
internal
, so
inner class Inner internal constructor()
, but that's module-private, not class-private.