Even when using Kotlin there may be times when you need to work with an existing Java class. You can still take advantage of your extension functions in this scenario. Learn how in this video.
Even when using Kotlin, there may be times when you need to work with an existing Java class. But we can still take advantage of our extension functions in that scenario as well. Here in our planet class, we have our extension property for getting the composition. Let's create a scratch file to test out how we could use this from a Java class. So we'll do that with the command shift n shortcut. And our file is going to be of type Java. So just like before, we're in a standard Java class and we're going to create a new class that we call sample use.
And we'll create a simple method in here of public void use extension. And this is where we're going to put the code in order to test out our Kotlin extension property that we created. So first we're going to need a planet. Let's just name it p. So we'll create a new one here in line six. And now let's try to call the planets composition method. Oh, but look, we don't get anything. Why is that? Well, the compiler doesn't know anything about this symbol, composition.
So let's try to figure out why that's happening. We can do that by looking at the b-compiled Java code of our planet class. So let's switch back over to there. And with quick actions, we're going to look at the Kotlin bite code. Now to make it easier to read, let's just click on b compiles so we can see a Java version of this particular file. Now as we scroll down, we come across the planet that Java class. And notice that it has fields for the id, name, inner, but you don't see anything about composition.
That's because the composition method, or property, is not part of the planet class. So where is it? Let's close the planet class down. If we scrolled a little bit more, we see another class. This one is planet KT and it contains a public static method called get composition. It takes the planet as the receiver and then it includes that logic that we had inside of our extension property. So this is how we can take advantage of Kotlin extensions from Java.
So let's switch back to our scratch pile to see that happen. So instead of calling composition directly, here on line seven we're going to call planet, Kt.getComposition, and then we're going to provide the planet as the receiver. And this is how we can get a planet's composition information. But the use of planet KT can be a bit off-putting, especially if you're not used to it.
So we do have another option available to us which lets us rename this file. To do that, we need to go back to our planet class. And right above where we have the package definition, we're going to use the at file annotation to rename it. So we're just going to hit enter here and we're going to type at file, JVN name, just ignore the errors for now. And we're going to call this planet utils.
Now I often have issues when I'm doing this and I get a bunch of weird imports. So I just cut what I have here, delete all of this, and then I paste it again right behind the add and it works. I have no idea why android studio behaves this way. So now let's switch back over to our scratch file. And notice how planet KT is no longer valid. However, planet utils is available.
So we can now change this to planet utils and this basically behaves in a way that we're very used to when we're working with utility functions inside of Java.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections