Skip to main content

Calling Kotlin from Java

2 min read
Share:
On this page (12sections)

Introduction

Calling Kotlin from Java is a fundamental concept every Kotlin developer should understand. Kotlin and Java compile to the same JVM bytecode, so you can mix both languages in one project with minimal friction.

Kotlin classes compile to JVM bytecode callable from Java. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Kotlin classes compile to JVM bytecode callable from Java.
  • Use @JvmStatic for static-like access from Java.
  • Use @JvmName to customize generated method names.

Syntax

@JvmStatic fun create() { }

Calling Kotlin from Java Example Program in Kotlin

class Factory {
    companion object {
        @JvmName("createUser")
        fun build(name: String) = "User: $name"
    }
}

fun main(args: Array<String>) {
    println(Factory.build("Anita"))
}

Sample Output

User: Anita

When to use

Use interop when migrating a Java codebase incrementally or calling mature Java libraries from Kotlin.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. fun build(name: String) = "User: $name" assigns or updates a value used later in the program.

  3. The println(Factory.build("Anita")) statement writes a line to the console — this produces part of the sample output below.

  4. Kotlin classes compile to JVM bytecode callable from Java.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Add @JvmStatic, @JvmOverloads, or @JvmField when Java callers need a cleaner API.
  • Use @file:JvmName to avoid awkward Kotlin-generated class names in Java.
  • Be explicit about nullability with @Nullable / @NonNull annotations for Java consumers.

Common Mistakes

  • Skipping the example and only reading the definition — hands-on practice cements the concept.
  • Copying syntax without understanding nullable vs non-nullable types or scope rules.
  • Ignoring compiler warnings that often point to safer alternatives.

Key Points

  • Kotlin classes compile to JVM bytecode callable from Java.
  • Use @JvmStatic for static-like access from Java.
  • Use @JvmName to customize generated method names.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Nullability annotations help Kotlin and Java agree on which values can be null at boundaries.
  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Calling Kotlin from Java in Kotlin?
Kotlin classes compile to JVM bytecode callable from Java.
When should I use Calling Kotlin from Java?
Use interop when migrating a Java codebase incrementally or calling mature Java libraries from Kotlin.
How is Calling Kotlin from Java different from Java?
Use @JvmName to customize generated method names.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials