# Static extension methods for Java classes?

**URL:** <https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190>\
**Category:** Uncategorized\
**Created:** [January 1, 2017, 12:05am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190 "2017-01-01T00:05:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [January 1, 2017, 12:05am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190/1 "2017-01-01T00:05:56Z")

</div>

Hi,

The `java.util.logging.Logger` class has a static `getLogger(String)` method. Say I wanted to add a `getLogger(Class)` method. I can’t do that, can I?

Rob

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 1, 2017, 2:18am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190/2 "2017-01-01T02:18:54Z")

</div>

No, this is not currently possible. There’s a YouTrack issue for this: [https://youtrack.jetbrains.com/issue/KT-11968](https://youtrack.jetbrains.com/issue/KT-11968)

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [June 12, 2019, 4:08am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190/3 "2019-06-12T04:08:14Z")

</div>

I actually had this exact question 30 minutes ago, so I started digging around and couldn’t find any solution or workaround for this, BUT while searching I found [this section](https://kotlinlang.org/docs/reference/extensions.html#nullable-receiver) on the Kotlinglang website that states that:

> Note that extensions can be defined with a nullable receiver type. Such extensions can be called on an object variable even if its value is null.

So then I had the craziest idea ever, why not define an extension function with a nullable receiver (without actually using that receiver) and then call it on a null object!  
So I tried that, and it worked pretty well, but it looked so ugly. It was like this:

```
(null as Type?).staticFunction(param1, param2)

```

So I went around that by creating a val in my extensions file of the receiver type that had a value of null and then use it in my other class.  
So, as an example, here is how I implemented a “static” extension function for the `Navigation` class in Android:  
In my NavigationExtensions.kt file:

```
val SNavigation: Navigation? = null
fun Navigation?.createNavigateOnClickListener(@IdRes resId: Int, args: Bundle? = null, navOptions: NavOptions? = null,
                                                navigationExtras: Navigator.Extras? = null) : (View) -> Unit {
    //This is just implementation details, don't worry too much about them, just focus on the Navigation? part in the method declaration
    return { view: View -> view.navigate(resId, args, navOptions, navigationExtras) }
}

```

In the code that uses it:

```
SNavigation.createNavigateOnClickListener(R.id.action_gameWonFragment_to_gameFragment)

```

Obviously, this isn’t a class name, it is just a variable of the class type that has a null value. This is obviously ugly on the extension maker side (because they have to create the variable) and on the developer side (because they have to use the `SType` format instead of the actual class name), but it is the closest that can be achieved right now compared to actual static functions. Hopefully, the Kotlin language makers will respond to the [issue](https://youtrack.jetbrains.com/issue/KT-11968) that was created and add that feature in the language.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [June 13, 2019, 2:34am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190/4 "2019-06-13T02:34:23Z")

</div>

If you are using a different receiver name, why not use an object with the methods? Like:

```auto
object SNavigation {
  fun createNavigationOnClickListener(@IdRes resId: Int,
                                      args: Bundle? = null,
                                      navOptions: NavOptions? = null,
                                      navigationExtras: Navigator.Extras? = null
                                    ) : (View) -> Unit {
    //This is just implementation details, don't worry too much about them, just focus on the Navigation? part in the method declaration
    return { view: View -> view.navigate(resId, args, navOptions, navigationExtras) }
}

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [June 13, 2019, 5:31am UTC](https://discuss.kotlinlang.org/t/static-extension-methods-for-java-classes/2190/5 "2019-06-13T05:31:10Z")

</div>

I actually thought about that right after I wrote the answer.  
I originally did the whole SNavigation thing so that I could then import it and change its name (by using "import [package].SNvaigation as Navigation) and then just use it instead of the Navigation class, but then I realized that in Kotlin you can’t access the static methods of a class from an instance (while in Java you can, although you get a lint warning), so I just edited my approach to use a different object name. Your answer is obviously more idiomatic and “Kotlinized,” so yeah, kudos on that.
