# Why private members of companion objects are visible to the class?

**URL:** <https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890>\
**Category:** Language Design\
**Created:** [October 6, 2017, 7:11am UTC](https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890 "2017-10-06T07:11:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vzasovin](https://avatars.discourse-cdn.com/v4/letter/v/ebca7d/32.png) [@vzasovin](https://discuss.kotlinlang.org/u/vzasovin)\
**Post date:** [October 6, 2017, 7:11am UTC](https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890/1 "2017-10-06T07:11:11Z")

</div>

I was a bit surprised when i found out that private members of companion objects are visible to the class, e.g to some member function within the class.

```kotlin
class A {
    
    companion object {
        
        private val x = 5
    }

    fun someMemberFunction() = x // x is visible here
}

```

But if we have an normal object, it’s private members are not visible outside of object itself (as i expected):

```kotlin
class A {
    
    object B {
        
        private val x = 5
    }

    fun someMemberFunction() = B.x // x is not visible here
}

```

How can we argue that these two very similar concepts differ in this way?

---

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [October 6, 2017, 7:39am UTC](https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890/2 "2017-10-06T07:39:41Z")

</div>

In Java, C#, C++, Ruby, Smalltalk, etc. for an instance method of a class some private static var inside the same class is also visible.

---

<div class="post-metadata">

**Author:** ![vzasovin](https://avatars.discourse-cdn.com/v4/letter/v/ebca7d/32.png) [@vzasovin](https://discuss.kotlinlang.org/u/vzasovin)\
**Post date:** [October 6, 2017, 8:40am UTC](https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890/3 "2017-10-06T08:40:20Z")

</div>

I begin to understand. We have access to private members of companion objects within a class because these objects are designed in particular to simulate static access through the class name. And companion object is initialized when the corresponding class is loaded. On the other hand normal nested object is initialized when accessed first time, i.e. it is not bound to the outer class.

---

<div class="post-metadata">

**Author:** ![Joaquin144](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joaquin144](https://discuss.kotlinlang.org/u/Joaquin144)\
**Post date:** [December 26, 2021, 4:07pm UTC](https://discuss.kotlinlang.org/t/why-private-members-of-companion-objects-are-visible-to-the-class/4890/4 "2021-12-26T16:07:10Z")

</div>

Thanks bro. I was having same doubt since long.
