Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 12 / Topic 5 of 7

@JvmStatic, @JvmOverloads, @JvmName — proven with javap

These three annotations exist for one reason: make a Kotlin API pleasant to call from Java. PricingApi.kt uses all three (@JvmName on the sibling file GstUtils.kt):

object PricingApi {
    private val legacy = LegacyCarrierRates()

    @JvmStatic
    @JvmOverloads
    fun quoteInr(city: String, weightKg: Double, expressSurchargePercent: Double = 0.0): Double {
        val ratePerKg = legacy.ratePerKgFor(city) ?: LegacyCarrierRates.flatRateInr()
        val base = ratePerKg * weightKg
        return base + base * expressSurchargePercent / 100
    }

    @JvmStatic
    fun carrierLabel(): String = "${legacy.carrierName()} (surge: ${legacy.isSurgeActive})"
}
@file:JvmName("GstUtils")   // without this, Java would see GstUtilsKt.withGst(...)
package com.stepshala.carrier

fun withGst(amount: Double): Double = amount * 1.18

@JvmStatic: real static methods, not INSTANCE.method()

A Kotlin object is a singleton; Java sees its members on a public INSTANCE field by default. @JvmStatic additionally generates a genuine static method on the class itself. javap -p on the compiled classes proves it — compare PricingApi (has @JvmStatic) against NoJvmStaticDemo (doesn't):

$ javap -p out/com/stepshala/carrier/PricingApi.class
public final class com.stepshala.carrier.PricingApi {
  public static final com.stepshala.carrier.PricingApi INSTANCE;
  public static final double quoteInr(java.lang.String, double, double);
  public static double quoteInr$default(java.lang.String, double, double, int, java.lang.Object);
  public static final java.lang.String carrierLabel();
  public static final double quoteInr(java.lang.String, double);
  ...
}

$ javap -p out/com/stepshala/carrier/NoJvmStaticDemo.class
public final class com.stepshala.carrier.NoJvmStaticDemo {
  public static final com.stepshala.carrier.NoJvmStaticDemo INSTANCE;
  public final java.lang.String greet();   // instance method ONLY -- Java must write INSTANCE.greet()
  ...
}

Without @JvmStatic, Java has to write PricingApi.INSTANCE.quoteInr(...). With it, Java writes the much more natural PricingApi.quoteInr(...) — and the plain companion method is still there underneath; @JvmStatic adds a forwarding static method, it doesn't remove the original.

@JvmOverloads: real overloads for every default-parameter suffix

quoteInr has one default parameter (expressSurchargePercent = 0.0). Without @JvmOverloads, Java would be forced to always pass all three arguments — Kotlin's default-parameter mechanism (the quoteInr$default method visible above) is a Kotlin-to-Kotlin convenience, invisible to Java callers, unless @JvmOverloads asks the compiler to also generate the "missing-trailing-argument" overloads for real:

public static final double quoteInr(java.lang.String, double, double);   // all 3 args
public static final double quoteInr(java.lang.String, double);           // drops the default
PricingApi.quoteInr("Pune", 4.0);          // uses the 2-arg overload
PricingApi.quoteInr("Pune", 4.0, 20.0);    // uses the 3-arg overload

Both really exist as separate, callable methods — not one method with a Java-side default, which the JVM doesn't support.

@JvmName: renaming the facade Java sees

Every Kotlin file with top-level functions compiles to a facade class named after the file, plus Kt — GstUtils.kt's functions would default to GstUtilsKt.withGst(...). @file:JvmName("GstUtils") overrides just that generated name:

$ javap -p out/com/stepshala/carrier/GstUtils.class
public final class com.stepshala.carrier.GstUtils {
  public static final double withGst(double);
}

No GstUtilsKt.class exists at all — the facade really is named GstUtils, so GstUtils.withGst(100.0) from Java reads naturally instead of carrying the Kt suffix. @JvmName also works on a single function (not just a whole file) to resolve a platform declaration clash — two Kotlin functions whose generic parameter types erase to the same JVM signature. Part 12's exercises 5 shows this exact clash, caused for real and then fixed; a second, standalone version lives in examples/platform-declaration-clash.kt, with the real compiler error (reproduced by deleting its one @JvmName line) quoted right in the file.

Full files: examples/interop-project/PricingApi.kt, GstUtils.kt, NoJvmStaticDemo.kt.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide