Javatpoint Logo
Javatpoint Logo

Java Interoperability: Calling Kotlin code from Java

As Kotlin is completely compatible with Java language. It means the application written in Java code can be easily called from Kotlin. In the similar way ,Kotlin code is also called from Java code.

Before discussing how to call Kotlin code from Java code, let's see how Kotlin file internally looks like.

How a simple Kotlin program internally looks like.

Let's create a simple main function in a MyKotlin.kt file.

After compiling the above Kotlin file MyKotlin.kt which internally looks like:

The Kotlin compiler internally adds a wrapper class with naming convention MyKotlinKt. The Kotlin file MyKotlin.kt is converted into MyKotlinKt and it is public in default. The default modifier of high level function is public and function is converted into static as default. As the return type is Unit in MyKotlin.kt, it is converted into void in MyKotlinKt.

Calling Kotlin code from Java code

MyKotlin.kt

MyJava.java

Output:

printing area inside Java class returning from Kotlin file: 20

Java code calling Kotlin file present inside package

If we want to call the Kotlin code from Java class both present inside the different packages, this requires to import the package name with Kotlin file name inside Java class and calling the Kotlin code from Java class. Another way is to give full path as packageName.KotlinFileKt.methodName().

MyKotlin.kt

MyJava.java

Output:

printing area inside Java class returning from Kotlin file: 20

Changing the Kotlin file name using annotation @JvmName

A Kotlin file name can be changed as wrapper class name using @JvmName annotation.

MyKotlin.kt

Write a Kotlin code and place annotation @file: JvmName("MyKotlinFileName") at the top. After compiling Kotlin code, the file name is changed into the name provided inside annotation (in my caseMyKotlinFileName). While accessing the code of MyKotlin.kt we require to use the file name as MyKotlinFileName.

MyJava.java

Output:

printing area inside Java class returning from Kotlin file: 20

Calling method of multiple file having same generated Java class name using@JvmMultifileClass

If the Kotlin's multiple files having same generated Java file name using @JvmName annotation, normally give error while calling from Java file. However, Kotlin compiler generates single Java façade class which contains generated Java file and all the declarations of the files which have same names. To active this generation faade, we use @JvmMultifileClass annotation in all the files.

MyKotlin1.kt

MyKotlin2.kt

MyJava.java

Output:

printing area inside Java class returning from Kotlin file: 20
printing volume inside Java class returning from Kotlin file: 120

Kotlin property access throughconst modifier

The Kotlin properties which are annotated with const modifier in the top level as well as in class are converted into static fields in Java. These properties are access from Java file as static properties called. For example:

MyKotlin.kt

MyJava.java

Output:

const 1
max 239
version 9






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA