# Static function in companion vs in a file

**URL:** <https://discuss.kotlinlang.org/t/static-function-in-companion-vs-in-a-file/9353>\
**Category:** Uncategorized\
**Created:** [September 7, 2018, 7:00pm UTC](https://discuss.kotlinlang.org/t/static-function-in-companion-vs-in-a-file/9353 "2018-09-07T19:00:27Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [September 7, 2018, 9:23pm UTC](https://discuss.kotlinlang.org/t/static-function-in-companion-vs-in-a-file/9353/2 "2018-09-07T21:23:44Z")

</div>

Top-level functions are more accessible: they are added to a package instead of a class. (private are added to the file)

Therefor, if you want a function to be very accible, it need to be a top-level function.  
This is the case if you would normally put it in an utility-class.

If you on the other hand don’t want the function to show up every time you press autocomplete, use companion-object. This is also for functions that must belong to the class, like create() or buildFrom(), because they are senseless if you call them without class.

This is just the way i use them

See also

> [@Companion object vs top level declaration](https://discuss.kotlinlang.org/t/companion-object-vs-top-level-declaration/7538):
>
> What are the benefits and issues of using a companion object vs using a top level function.

---

_[View the full topic](https://discuss.kotlinlang.org/t/static-function-in-companion-vs-in-a-file/9353)._
