# What is the advantage of "companion object" vs static keyword

**URL:** <https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034>\
**Category:** Language Design\
**Created:** [August 4, 2017, 4:25pm UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034 "2017-08-04T16:25:28Z")\
**Posts on this page:** 8\
**Page:** 6

<div class="post-metadata">

**Author:** ![DonWills](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@DonWills](https://discuss.kotlinlang.org/u/DonWills)\
**Post date:** [June 18, 2018, 3:40pm UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/104 "2018-06-18T15:40:52Z")

</div>

No. I meant within the same language. For example for C, the transitions from K&R C to C90 to C99 to C11, with similar transitions for Fortran 77, Fortran 90, and for Java 1, 2, 3, 4, 5, 6, 7, 8, 9. AFAIK, none of these transitions broke source code. Yes, you may have needed to use compiler switches to stop warnings, but the source code was upward compatible. Swift 3 was not source compatible with Swift 2, and Swift 2 was not source compatible with Swift 1. I believe Swift 4 source is “largely compatible” with Swift 3, but that’s like pregnancy - you either are or you are not.

And AFAIK, unlike Objective-C, C++ was never upward compatible with C - it was similar, but not compatible.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [June 18, 2018, 5:35pm UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/105 "2018-06-18T17:35:50Z")

</div>

> [@DonWills](#):
>
> No. I meant within the same language

And Kotlin is not the same language as Java and Kotlin does at least maintain compatibility since 1.0 (it actually did not prior to 1.0). So your point does not seem relevant.

> [@DonWills](#):
>
> C++ was never upward compatible with C - it was similar, but not compatible.

It was compatible enough that I was not going to split hairs with you on the level of compatibility. I would certainly agree that compatibility or similarity was indeed important for the C to C++ transition and does support your argument, but that is really the only example that I see.

---

<div class="post-metadata">

**Author:** ![Radzell](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Radzell](https://discuss.kotlinlang.org/u/Radzell)\
**Post date:** [June 18, 2018, 6:42pm UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/106 "2018-06-18T18:42:19Z")

</div>

> [@DonWills](#):
>
> For Kotlin to not repeat that mistake, companion objects must stay the way they are, or be modified in a manner that is upward compatible. Quite frankly, I can’t imagine any changes that wouldn’t just dig a deeper hole, so my recommendation is leave it alone.

Well the kotlin language lead has said many times they have been thinking about removing companion objects because they’ve been used like statics.

---

<div class="post-metadata">

**Author:** ![nemo](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nemo](https://discuss.kotlinlang.org/u/nemo)\
**Post date:** [July 4, 2018, 4:28am UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/107 "2018-07-04T04:28:59Z")

</div>

Kotlin no static keyword, so they use companion object instant of static keyword, if you want to use static keyword as Java code must be use annotation in Kotlin to know static keyword.  
Kotlin:

1. Create class type object no need companion keyword  
object NetworkUtil {  
fun callMe(){}  
}  
we can call direct from class object.  
access: NetworkUtil.callMe()
2. Create class by create companion object inside class  
class NetworkUtil{  
companion object {  
fun callMe() {  
}  
}  
}  
access: NetworkUtil.callMe()
3. Create class type object after add Annotation keyword top of fun want to use.  
object NetworkUtil {  
@JvmStatic  
fun callMe(){}  
}  
access: NetworkUtil.callMe()

---

<div class="post-metadata">

**Author:** ![alexjuca](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexjuca/32/5443_2.png) [@alexjuca](https://discuss.kotlinlang.org/u/alexjuca)\
**Post date:** [August 17, 2018, 10:38pm UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/108 "2018-08-17T22:38:07Z")

</div>

Is this not the way that we define companion object methods?

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [August 18, 2018, 12:37am UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/109 "2018-08-18T00:37:29Z")

</div>

Check out this topic: [Please close the mega topics (ternary and companion objects)](https://discuss.kotlinlang.org/t/please-close-the-mega-topics-ternary-and-companion-objects/8191)

This recent post is a post in response to the 35th post in this thread and is a really good example of how a mega thread isn’t able to end naturally:

> **The topics aren’t ending naturally**  
> These topics are interesting and people will have an opinion on them. This is great until the topic becomes a place to petition for/against a position. Even though everything has been said, another user will come in support/against the feature of discussion without communicating anything new.

The original link is far enough up in the thread history that there’s a good chance people won’t see it and resume commenting on this thread.

Reposting hoping [this link](https://discuss.kotlinlang.org/t/please-close-the-mega-topics-ternary-and-companion-objects/8191) remains at the bottom 🙂

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [August 22, 2018, 10:35am UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/110 "2018-08-22T10:35:33Z")

</div>

Thank you, we have [decided to close this topic](https://discuss.kotlinlang.org/t/announcement-about-closing-two-topics-and-a-code-of-conduct-reminder/9160). You can read the quick summary [here](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/99).

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [August 22, 2018, 10:35am UTC](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034/111 "2018-08-22T10:35:40Z")

</div>



[Previous page](https://discuss.kotlinlang.org/t/what-is-the-advantage-of-companion-object-vs-static-keyword/4034.md?page=5)
