0
0
Kotlinprogramming~10 mins

Kotlin annotations for Java callers (@JvmStatic, @JvmField) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Kotlin annotations for Java callers (@JvmStatic, @JvmField)
Define Kotlin class/object
Add @JvmStatic or @JvmField
Compile Kotlin code
Java code calls Kotlin members
@JvmStatic: static method in Java
@JvmField: direct field access in Java
Shows how adding @JvmStatic or @JvmField changes how Java code can call Kotlin members as static methods or direct fields.
Execution Sample
Kotlin
object Utils {
    @JvmStatic
    fun greet() = "Hello"

    @JvmField
    val version = 1
}
Defines a Kotlin object with a static method and a field accessible directly from Java.
Execution Table
StepActionKotlin MemberAnnotation EffectJava Call SyntaxResult
1Define object UtilsUtilsN/AN/AObject created with members
2Add @JvmStatic to greet()greet()Generates static method in JavaUtils.greet()"Hello" returned
3Add @JvmField to versionversionGenerates public static field in JavaUtils.versionValue 1 accessed directly
4Java calls Utils.greet()greet()Static method callUtils.greet()"Hello" returned
5Java accesses Utils.versionversionDirect field accessUtils.versionValue 1 accessed
6No annotation on other membersotherMemberNo static or direct fieldUtils.getOtherMember()Needs getter method
7End of exampleN/AN/AN/AExecution complete
💡 All annotated members are accessible as static or direct fields from Java; others use default Kotlin accessors.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Utils.greet()Not definedStatic method generatedStatic method unchangedStatic method available
Utils.versionNot definedNot definedStatic field generatedStatic field available
Key Moments - 3 Insights
Why does @JvmStatic make a Kotlin function callable as a static method in Java?
Because @JvmStatic tells the compiler to generate a static method in the Java bytecode, so Java can call it without an instance, as shown in execution_table step 2 and 4.
What difference does @JvmField make for Kotlin properties when accessed from Java?
It exposes the property as a public static field directly, skipping getter methods. See execution_table step 3 and 5 where Utils.version is accessed as a field.
What happens if you don't use these annotations on Kotlin members called from Java?
Java must use getter methods to access properties or instance methods, not static calls or direct fields, as noted in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what Java syntax calls the Kotlin function annotated with @JvmStatic?
AUtils.INSTANCE.greet()
Bnew Utils().greet()
CUtils.greet()
DUtils.getGreet()
💡 Hint
See execution_table row 4 showing Java calls Utils.greet() as a static method.
At which step does the Kotlin property become accessible as a direct static field in Java?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Check execution_table row 3 where @JvmField generates the static field.
If you remove @JvmStatic from greet(), how would Java call change?
AJava calls Utils.greet() as static method
BJava calls Utils.INSTANCE.greet() as instance method
CJava calls Utils.getGreet()
DJava cannot call greet()
💡 Hint
Refer to key_moments about default Kotlin access from Java without @JvmStatic.
Concept Snapshot
Kotlin @JvmStatic annotation makes functions callable as static methods from Java.
@JvmField exposes Kotlin properties as public static fields in Java.
Without these, Java uses instance methods or getters.
Use in objects or companion objects for Java interoperability.
Improves Java code simplicity and performance.
Full Transcript
This visual execution shows how Kotlin annotations @JvmStatic and @JvmField affect Java interoperability. When you add @JvmStatic to a Kotlin function inside an object, the compiler generates a static method in Java, so Java code can call it directly without an instance. Similarly, @JvmField on a Kotlin property creates a public static field in Java, allowing direct access without getter methods. The execution table traces defining the Kotlin object, adding annotations, compiling, and how Java calls these members. Variables track the presence of static methods and fields after each step. Key moments clarify why these annotations matter and what happens without them. The quiz tests understanding of Java call syntax and annotation effects. The snapshot summarizes usage and benefits for Java callers. This helps beginners see exactly how Kotlin annotations change Java access step-by-step.