Java: How do I get along with Kotlin ?

Ashwin S
AndroidPub
Published in
3 min readMar 31, 2018

An Android developer knows better how much Kotlin has infiltrated the code bases of popular projects today. And with developers slowly turning to Kotlin for development, the question that still remains is, how to go about it? Now there are 3 ways to look at it. One, you have to completely start a new workspace for your project which will be weaved with the magic of Kotlin. Well, good luck with that if time permits. Second, you have to completely migrate the old project to Kotlin. The IntelliJ Idea comes with a good support for the code conversion but what might be simple for a small project with less complexity might not be simple for the large ones. Alright the third one! Well its more like a temporary patch of the new Kotlin code and the old Java code together in the same project till we actually find a time for way number 1. We all know that there is an open window of possibility for both Java and Kotlin to get along and know each other well. That’s right you read my mind, it’s Kotlin’s magical interoperability with Java.

Now we all Kotlin enthusiasts might have come across this blog on how to access Java code in Kotlin which is fairly easily considering one of the goals of Kotlin was Java interoperability. Now the tricky part is calling a Kotlin member in a Java code. The official blog might have highlighted the basic things to keep in mind while playing with the interoperability but then I am here to revise a few and also throw light on the things which might have got missed from the official blog.

The data classes?

Well for this we have to make the object absorb the data using the Primary constructor instead of the traditional setter as,

The singletons?

Now I don’t have to explain how useful Singletons are in saving us from the memory eating object clones. And Kotlin makes it even easier with the object datatype getting rid of the burden of implementing the static getInstance() ourselves.

Reference this beauty in Java with a simple INSTANCE keyword which acts as single point reference to that singleton.

The Companions?

Well, the Kotlin Companions have done so well to make us almost forget that subclasses and static members even existed. But is there a way to refer that in Java? Well hell yes!

The Companion keyword acts as that single point reference to the subclass or static members like almost the last case as,

The extension functions?

The extension functions are like the prayers answered from above for the need of extending or accessing a utility function to an existing class in the project without the need to create a class for it. And guess what you can create a file containing all the extension functions to extend from which can be extended or accessed by any of your java classes in your project. Guessing how?

Well, a class is created for you as name_of_your_class_containing_the_extension_fun+”Kt” with which you can access your extension functions in Java on the go. And the object with which the function was called in Kotlin will be passed as a parameter in Java as,

An internal headache?

Well, it’s a new visibility modifier introduced in Kotlin giving module level visibility. But how does Java take it? Can it? Well duh, of course, it would!

The java part takes care of the hard work of understanding the modular level visible function only if we append the name of the function call with “production_sources_for_module_app” as,

Well, these examples should be enough to convince yourselves to at least start writing that new code of yours in Kotlin and exploiting it’s advantages in your largely complex java project.

Expect a follow-up post once I have done pushing more Kotlin features into my java project.

--

--