Calling kotlin Code from Java Code

Kururu
4 min readDec 18, 2022

--

Intro

The best thing about Kotlin is that it is designed with Java interoperability in mind. It means that the existing Java code can be called from Kotlin, and also the Kotlin code can be called from Java. Both ways are supported.

Kotlin code can be easily called from Java. For example, instances of a Kotlin class can be seamlessly created and operated in Java methods.but there is many differences require attention when calling kotlin code inside java.

in Android if you use kotlin you can still access all APIs made with Java without any problem.

How can you called Kotlin Code from Java?

Kotlin code compiled to JVM elements , hence very easy to access it from java code.

but you can add some annotations to your kotlin code to make it easier exposed from java code.

Annotations are means of attaching metadata to code.

lets begin, open Intellij Idea create new Kotlin project , select gradle for project.

if you want to add java sources root right click on main directory and new Directory add java.

and now let start learn about some annotation that make easy to access kotlin code from java

@JvmStatic

this used to annotate kotlin static fields and function so you can access them from java code without need to add INSTANCE keyword after class name.

public object  User{


@JvmStatic //use this
public fun getUserName():String="Kururu"
}

and now from java code you can access getUserName static function

public class JavaCode {

public static void main(String[] args) {
System.out.println(User.getUserName()); // without INSTANCE KEYWORD

// System.out.println(new Employee()
// .getEmployeeName());
}
}

you can also annotate static field with JvmStatic

public object  User{

@JvmStatic
var name:String ="Kururu"
}

and access it as static function. without INSTANCE keyword.

@JvmField

this used to access instance fields in normal class , we use object in previous example.

but you may think on how to access fields on normal Kotlin classes

the answer is using @JvmField annotation

before jump to code , lets explain how kotlin fields or properties compiled?

A Kotlin property is compiled to the following Java elements:

  • a getter method, with the name calculated by prepending the get prefix
  • a setter method, with the name calculated by prepending the set prefix (only for var properties).

for example:

var firstName: String

this line of compiled to following:

private String firstName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

so to access kotlin fields from java without this annotation you do make this

new ClassName().getFirstName();

by using @JvmField annotation you can access the field name directly without caring about getter and setter names.

Notice that we have used @JvmField over the field name to instruct the Kotlin compiler not to generate any getter and setter for the same and expose it as a field

class Employee{

@JvmField //from java code you can access firstName
var firstName:String="Kururu"

}

java code:

public static void main(String[] args) {


System.out.println(new Employee().firstName
);
}

@ JvmName

this annotation tells JVM to associate specific name to your function , this will override the kotlin code’s function name.

kotlin:

@JvmName("getEmployeeName")
fun getEmpName():String{
return "Abdo";
}

java:

public class JavaCode {

public static void main(String[] args) {
// System.out.println(User.getMyUserName());

System.out.println(new Employee().getEmployeeName()
);
}
}

@ JvmOverloads

Normally, if you write a Kotlin function with default parameter values, it will be visible in Java only as a full signature, with all parameters present. If you wish to expose multiple overloads to Java callers, you can use the @JvmOverloads annotation.

The annotation also works for constructors, static methods, and so on. It can’t be used on abstract methods, including methods defined in interfaces.

so this annotation use to indicate default parameters so can skip them from java code , without this you must pass all params.

this applicable for class constructors .

Kotlin:

@JvmOverloads fun getEmpName( name:String="abdo"):String{
return "Abdo";
}

java:

   public static void main(String[] args) {
// System.out.println(User.getMyUserName());


//access getEmpName() without need to pass NAME argument
System.out.println(new Employee().getEmployeeName()
);
}

thanks to Amit Shekhar who always try to simplify thing to us

this topic inspired by his blogs

check his blogs: https://amitshekhar.me

references:

https://kotlinlang.org/docs/java-to-kotlin-interop.html

https://amitshekhar.me

--

--