Leveraging Java-Kotlin interoperability to use extension functions in Java

Sankavi Raj
Version 1
Published in
3 min readOct 2, 2023

In my Kotlin journey, I came across something called an extension function and about how Java doesn’t have this feature. But looking into it more I kept thinking “ok but how is this different from static methods in java?”.

Static methods vs. Extension functions

Extension functions are a feature in Kotlin that allows you to add new functions to existing classes without modifying their source code. They enhance code readability and expressiveness by providing the illusion of directly adding methods to classes.

  1. Syntax: In Kotlin, extension functions are defined outside the class they extend. They use the this keyword to access the class they extend. Here's a simple example that adds a square() function to the int class:
fun Int.squared(): Int {
return this * this
}
  1. Syntax: In Kotlin, extension functions are defined outside the class they extend. They use the this keyword to access the class they extend. Here's a simple example that adds a square() function to the int class:
  2. Access: Extension functions can access both public and private members of the class they extend, making them powerful for adding new behaviour.
  1. Usage: They are used to enhance existing classes with new methods, making code more readable and expressive.

On the contrary, Static methods are a feature in languages like Java and C#. They allow you to define methods within a class that are associated with the class itself rather than instances of the class.

  1. Syntax: Static methods are defined within a class using the static keyword in languages like Java. They do not use the this keyword because they are not associated with instances of the class.
    CODE SNIPPET
  2. Access: Static methods can only access other static members (fields and methods) of the class. They cannot directly access instance-specific data.
  3. Usage: Static methods are used for utility functions or operations that don’t require access to instance-specific data. They are often used for helper functions that don’t conceptually belong to a specific instance.

Ok, so the main takeaway is that you can add an extension function to extend a class without having to define it as a static function so that way you have access to all the class members. But what does that have to do with Java?

Photo by Josh Redd on Unsplash

Interoperability between Java and Kotlin

Kotlin was designed to run on the JVM, making it a natural fit for Java projects. Kotlin compiles to regular Java bytecode, making Kotlin classes and functions accessible from Java code. You can also access your Java classes from Kotlin. So how will this work with our extension function? Let’s use our square() function:
CODE SNIPPET OF EXAMPLE HERE //todo

Extension functions and static methods are valuable tools for enhancing classes and adding functionality. With Kotlin’s exceptional interoperability with Java, you have the flexibility to choose between extension functions and static methods based on your project’s requirements and your team’s preferences, creating a dynamic and versatile development environment.

About the Author:
Sankavi Raj is a Java Developer here at Version 1.

--

--