# Extension function for Java class

**URL:** <https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067>\
**Category:** Support\
**Created:** [January 8, 2019, 12:37pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067 "2019-01-08T12:37:43Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 8, 2019, 12:37pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/1 "2019-01-08T12:37:43Z")

</div>

There is a Java class.

> class JavaClass {  
> … a lot of static methods…  
> }

Can I create an extension function in my Kotlin code and call it like `JavaClass.extensionFunction()`?

I tried `fun JavaClass.Companion.extensionFunction()` but, obviously, the `Companion` cannot be found!

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [January 8, 2019, 12:56pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/2 "2019-01-08T12:56:24Z")

</div>

Obviously you can’t extend the thing that does not exist. You will get the same error if you want to non-existing companion of kotlin class. There is a feature request somewhere in the tracker to automatically create empty companions in such cases, but it probably won’t work for java classes. Does it matter to you what namespace will you use to call static methods?

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 8, 2019, 1:32pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/3 "2019-01-08T13:32:05Z")

</div>

> Does it matter to you what namespace will you use to call static methods?

I want this extension function (in a Kotlin file) to seem like a static function (in a Java class).  
There is no reason to do it but I am clearly experimenting around with Kotlin and I am wondering if this is possible.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [January 8, 2019, 1:33pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/4 "2019-01-08T13:33:33Z")

</div>

Kotlin extensions are not visible from java anyway.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 8, 2019, 1:45pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/5 "2019-01-08T13:45:50Z")

</div>

Well actually they are. Assuming you wrote the extension function in a plain file outside a class, the compiler creates a class called `<filename>Kt` and the function will be a static function with its first argument the receiver. When using Kotlin in a (decent) IDE, it detects that and hides the first parameter and lets you call as a normal function.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [January 8, 2019, 1:48pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/6 "2019-01-08T13:48:41Z")

</div>

I mean that they are not visible as extensions. The idea was to upgrade Java class with some functionality. This obviously won’t work. Maybe some IDE cheats could do that, but the code itself will be really ugly.

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 8, 2019, 2:21pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/7 "2019-01-08T14:21:10Z")

</div>

I will not call the extension function from Java. I will call the function only from Kotlin. I wanted to seem (only to seem) like it is belonging in a Java class. I want to create an extension function and call it from Kotlin source only. As I know of, the extension functions can be called like this `classInstance.functionName()`. I want to create an extension function for a class and call it `ClassName.functionName()`. I want the extension function to resolve statically (as already does) and call it in a static way in my Kotlin source.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 8, 2019, 5:06pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/8 "2019-01-08T17:06:46Z")

</div>

Well then what you want is a [plain regular extension function](https://kotlinlang.org/docs/reference/extensions.html). Say you have you Java class in a `.java` file:

```auto
public class MyJavaClass {

    public String derp(){
        return "Hey mom!"
    }

    public MyJavaClass(){}
    
    // whatever...

}

```

In any Kotlin `.kt` file just write:

```auto
fun MyJavaClass.myExtension(){ // <-- here's the extension function definition. 
                               // Notice your java class as receiver
    println("Hey! I'm an extension! Look: ${derp()}")
}

fun main(){
    val myJ = MyJavaClass()
    myJ.myExtension()
}

```

Output will be `Hey! I'm an extension! Look: Hey mom!`

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 8, 2019, 6:14pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/9 "2019-01-08T18:14:14Z")

</div>

Based on your example I could explain myself better.  
I don’t want to call the extension function like `MyJavaClass().myExtension()`. But, I want to call the extension function like `MyJavaClass.myExteension()` if possible.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 8, 2019, 8:46pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/10 "2019-01-08T20:46:52Z")

</div>

Indeed in the definition i wrote the receiver is the class not a particular instance of that class. On every object created from `MyJavaClass` you will be able to call `myExtension()`

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 8, 2019, 9:04pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/11 "2019-01-08T21:04:14Z")

</div>

According to your example is it possible to create something like below?

> fun main(){  
> //val myJ = MyJavaClass()  
> [//myJ.myExtension](https://myJ.myExtension)()  
> MyJavaClass.myExtension()  
> }

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 8, 2019, 9:26pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/12 "2019-01-08T21:26:43Z")

</div>

I think you have some sort of confusion on how classes and objects works. I’ll try to help. Have a look here:

```auto
fun main(){
    val myJ = MyJavaClass()
    myJ.myExtension() // <-- this is correct. Keep in mind that you have to declare 
                      // `myExtension()` somewhere as i did in the previous example

    MyJavaClass.myExtension() // <-- this line is incorrect. You are calling a method
                              // as it was static, but it's not.
}

```

Have a look at a more complete example:

```auto
// JAVA HERE

public class A {

    public String foo;

    public A(){
    }

}

// KOTLIN HERE

data class B(val foo: String)

fun A.extensionAlpha(){
    println("Hey I'm A and foo = $foo")
}

fun B.extensionBeta(){
    println("Hey I'm B and foo = $foo")
}

fun main() {
    val a = A()
    a.foo = "A"
    val b = B("B")
    a.extensionAlpha()
    b.extensionBeta()
}

```

The output will be:

```auto
Hey I'm A and foo = A
Hey I'm B and foo = B

```

You could even compact all of this this way (writing all in Kotlin so it’s more compact but it would work seamlessly using classes/interfaces from Java):

```auto
interface Foo {
    val foo: String
}

data class A(override val foo: String = "A"): Foo

data class B(override val foo: String = "A"): Foo

fun Foo.extension(){
    println("Hey I'm ${this::class.java.simpleName} and foo = $foo")
}

fun main() {
    val a = A()
    val b = B()
    a.extension()
    b.extension()
}

```

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 9, 2019, 4:19am UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/13 "2019-01-09T04:19:35Z")

</div>

I understand that.  
To answer to my self about

> Can I create an extension function in my Kotlin \>code and call it like \>`JavaClass.extensionFunction()` ?

the answer is no. There is no way to implement something like that.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 9, 2019, 12:09pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/14 "2019-01-09T12:09:24Z")

</div>

Yeah, but what’s the point of being able to add a static function to a class? Just create a static function somewhere else and you are good to go.

---

<div class="post-metadata">

**Author:** ![BILLyTheLiTTle](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/billythelittle/32/4759_2.png) [@BILLyTheLiTTle](https://discuss.kotlinlang.org/u/BILLyTheLiTTle)\
**Post date:** [January 9, 2019, 12:41pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/15 "2019-01-09T12:41:06Z")

</div>

If you could add a static function to a class it will be bonded to that class and it will not “pollute” the namespace at the package you are working. Probably, if a `Companion` object could be created automatically (as @darksnake mentioned) , this could be achieved.  
I asked just to assure if this was not possible or if I have missed something!

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [January 9, 2019, 1:00pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/16 "2019-01-09T13:00:08Z")

</div>

It is not possible unless the class already has a companion. In that case: `fun Foo.Companion.bar() = "baz"`

---

<div class="post-metadata">

**Author:** ![marek.shape](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@marek.shape](https://discuss.kotlinlang.org/u/marek.shape)\
**Post date:** [January 11, 2019, 7:51pm UTC](https://discuss.kotlinlang.org/t/extension-function-for-java-class/11067/17 "2019-01-11T19:51:06Z")

</div>

You’re looking for this feature in the tracker  
[https://youtrack.jetbrains.com/issue/KT-11968](https://youtrack.jetbrains.com/issue/KT-11968)
