# Kotlin: Constructor of inner class nested can be called only with receiver of containing class

**URL:** <https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700>\
**Category:** Support\
**Created:** [May 6, 2018, 10:14am UTC](https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700 "2018-05-06T10:14:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vampzzz](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vampzzz](https://discuss.kotlinlang.org/u/vampzzz)\
**Post date:** [May 6, 2018, 10:14am UTC](https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700/1 "2018-05-06T10:14:48Z")

</div>

Hi, I’m have created nested class but now i can’t access it properly its giving me error i have tried addiing keyword

> Public

it didn’t work i don’t know where i’m wrong when checking from kotlin original website i found this code.

```
//Code from the kotlin website
        class Outer {
            private val bar: Int = 1
            inner class Inner {
                fun foo() = bar
            }
        }

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

```

i did same kind of thing but with `String` but my code is giving me error.  
//My Code

```
class Outer{

    private val name:String?=null

    inner class Nested{
         fun show(){

            println("Nested")
            //println(name) this won't run until we add word "inner" with nested class

            //now we have added inner now "name" can be accessed
             println(name)

        }
    }

}

fun main(args:Array<String>){

    var obj = Outer() //outer class instance created
    val obj2 = Outer.Nested() //Here instance of nested class in created
    obj2.show()
}

```

When i don’t use keyword `inner` with the `Nested` class then its working fine but without it its not working can someone please explain it a bit thanks.

**Error:**

> Error:(23, 22) Kotlin: Constructor of inner class nested can be called only with receiver of containing class

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 6, 2018, 1:46pm UTC](https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700/2 "2018-05-06T13:46:19Z")

</div>

[https://kotlinlang.org/docs/reference/nested-classes.html#inner-classes](https://kotlinlang.org/docs/reference/nested-classes.html#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

Therefore you have to call the constructor of an inner class like this

```kotlin
Outer().Nested()

```

otherwise the inner class could not store a reference to the outer class.

---

<div class="post-metadata">

**Author:** ![vampzzz](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vampzzz](https://discuss.kotlinlang.org/u/vampzzz)\
**Post date:** [May 9, 2018, 11:01am UTC](https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700/3 "2018-05-09T11:01:05Z")

</div>

Thanks a lot for your help i got it thanks.

---

<div class="post-metadata">

**Author:** ![lpl0129](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lpl0129](https://discuss.kotlinlang.org/u/lpl0129)\
**Post date:** [June 18, 2019, 7:41am UTC](https://discuss.kotlinlang.org/t/kotlin-constructor-of-inner-class-nested-can-be-called-only-with-receiver-of-containing-class/7700/4 "2019-06-18T07:41:52Z")

</div>

In kotlin, you need to call the external class constructor at the same time

val obj2=Outer().Nested()
