Bird
Raised Fist0
Kotlinprogramming~5 mins

Kotlin annotations for Java callers (@JvmStatic, @JvmField) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the @JvmStatic annotation do in Kotlin?
It makes a Kotlin function or property appear as a static method or field in Java, allowing Java code to call it without needing an instance.
Click to reveal answer
beginner
How does @JvmField affect Kotlin properties when accessed from Java?
It exposes the Kotlin property as a public Java field, removing the getter/setter methods and allowing direct field access from Java.
Click to reveal answer
intermediate
When should you use @JvmStatic in Kotlin companion objects?
Use <code>@JvmStatic</code> on functions or properties inside companion objects to let Java callers access them as static members of the containing class.
Click to reveal answer
intermediate
What is the difference between a Kotlin property with @JvmField and a regular Kotlin property without it when accessed from Java?
With @JvmField, Java code accesses the property as a field directly. Without it, Java accesses it via getter/setter methods.
Click to reveal answer
advanced
Can @JvmStatic be used on top-level Kotlin functions?
No, @JvmStatic is only applicable inside companion objects or named objects. Top-level functions are already static in Java bytecode.
Click to reveal answer
What does @JvmStatic do when applied to a function inside a Kotlin companion object?
ARemoves the function from the bytecode
BMakes the function callable as a static method from Java
CConverts the function to an extension function
DMakes the function private
Which annotation exposes a Kotlin property as a public field in Java, skipping getter/setter methods?
A@JvmField
B@JvmStatic
C@JvmOverloads
D@JvmName
Can @JvmStatic be used on top-level Kotlin functions to make them static in Java?
ANo, it only works inside companion objects or named objects
BYes, always
CYes, but only for private functions
DNo, it only works on properties
What happens if you do NOT use @JvmField on a Kotlin property accessed from Java?
AJava accesses it as a static field
BThe property is private in Java
CJava cannot access the property
DJava accesses it via getter/setter methods
Which Kotlin annotation helps Java callers access companion object members as static members of the class?
A@JvmField
B@JvmSuppressWildcards
C@JvmStatic
D@JvmSynthetic
Explain how @JvmStatic and @JvmField improve Kotlin and Java interoperability.
Think about how Java expects static methods and fields.
You got /4 concepts.
    Describe when and why you would use @JvmStatic inside a Kotlin companion object.
    Consider Java's static method calling style.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of the @JvmStatic annotation in Kotlin when called from Java?
      easy
      A. To convert Kotlin lambdas into Java anonymous classes
      B. To expose Kotlin properties as public fields in Java
      C. To make a Kotlin function in a companion object appear as a static method in Java
      D. To mark a Kotlin class as a Java interface

      Solution

      1. Step 1: Understand the role of @JvmStatic

        @JvmStatic makes functions inside Kotlin companion objects look like static methods in Java, so Java code can call them without an instance.
      2. Step 2: Differentiate from @JvmField

        @JvmField exposes properties as fields, not functions. The question asks about functions, so @JvmStatic is correct.
      3. Final Answer:

        To make a Kotlin function in a companion object appear as a static method in Java -> Option C
      4. Quick Check:

        @JvmStatic = static method in Java [OK]
      Hint: Remember: @JvmStatic = static method in Java [OK]
      Common Mistakes:
      • Confusing @JvmStatic with @JvmField
      • Thinking @JvmStatic applies to properties
      • Assuming it changes Kotlin class types
      2. Which of the following is the correct way to expose a Kotlin property as a public field accessible directly from Java?
      easy
      A. Annotate the property with @JvmStatic
      B. Annotate the property with @JvmField
      C. Declare the property as val without annotations
      D. Use @JvmOverloads annotation

      Solution

      1. Step 1: Identify the annotation for exposing fields

        @JvmField exposes Kotlin properties as public fields for direct Java access, bypassing getters/setters.
      2. Step 2: Eliminate other options

        @JvmStatic applies to functions, not properties. @JvmOverloads is for default parameters. val alone does not expose as field.
      3. Final Answer:

        Annotate the property with @JvmField -> Option B
      4. Quick Check:

        @JvmField = public field in Java [OK]
      Hint: Use @JvmField to expose properties as fields [OK]
      Common Mistakes:
      • Using @JvmStatic on properties
      • Assuming val exposes public field automatically
      • Confusing @JvmOverloads with field exposure
      3. Given the Kotlin code below, what will be the output when called from Java?
      class Example {
          companion object {
              @JvmStatic
              fun greet() = "Hello from static"
      
              fun greetInstance() = "Hello from instance"
          }
      }
      
      // Java code:
      System.out.println(Example.greet());
      System.out.println(Example.Companion.greetInstance());
      medium
      A. Compilation error\nHello from instance
      B. Hello from static\nCompilation error
      C. Compilation error\nCompilation error
      D. Hello from static\nHello from instance

      Solution

      1. Step 1: Understand @JvmStatic effect on Java calls

        @JvmStatic makes greet() callable as a static method on Example class directly from Java.
      2. Step 2: Understand companion object function call

        greetInstance() is not annotated, so Java must call it via Example.Companion instance.
      3. Final Answer:

        Hello from static\nHello from instance -> Option D
      4. Quick Check:

        @JvmStatic allows direct static call, others via Companion [OK]
      Hint: @JvmStatic = call directly on class; else use Companion [OK]
      Common Mistakes:
      • Assuming all companion functions are static in Java
      • Trying to call unannotated functions as static
      • Ignoring the Companion object in Java calls
      4. Identify the error in the Kotlin code below if the goal is to allow Java code to access count as a public field:
      class Counter {
          companion object {
              var count = 0
          }
      }
      medium
      A. Missing @JvmField on count
      B. Missing @JvmStatic on count
      C. Companion object cannot have properties
      D. count must be declared as val not var

      Solution

      1. Step 1: Understand property exposure to Java

        To expose a Kotlin property as a public field in Java, it must be annotated with @JvmField.
      2. Step 2: Check annotations and property modifiers

        @JvmStatic applies to functions, not properties. Companion objects can have properties. var is allowed.
      3. Final Answer:

        Missing @JvmField on count -> Option A
      4. Quick Check:

        Expose property as field = use @JvmField [OK]
      Hint: Use @JvmField on properties for Java field access [OK]
      Common Mistakes:
      • Using @JvmStatic on properties
      • Thinking companion objects can't have properties
      • Confusing var and val for Java exposure
      5. You want to create a Kotlin utility class with a companion object that has a constant integer MAX accessible as a static field from Java, and a function printMax() callable as a static method from Java. Which is the correct Kotlin code?
      hard
      A. class Utils { companion object { @JvmField val MAX = 100 @JvmStatic fun printMax() = println(MAX) } }
      B. class Utils { companion object { val MAX = 100 fun printMax() = println(MAX) } }
      C. class Utils { companion object { @JvmStatic val MAX = 100 @JvmField fun printMax() = println(MAX) } }
      D. class Utils { companion object { const val MAX = 100 fun printMax() = println(MAX) } }

      Solution

      1. Step 1: Expose constant as static field

        Use @JvmField on val MAX to expose it as a static field in Java.
      2. Step 2: Expose function as static method

        Use @JvmStatic on printMax() to make it callable as a static method from Java.
      3. Step 3: Verify other options

        class Utils { companion object { val MAX = 100 fun printMax() = println(MAX) } } lacks annotations, so Java calls require Companion. class Utils { companion object { @JvmStatic val MAX = 100 @JvmField fun printMax() = println(MAX) } } swaps annotations incorrectly. class Utils { companion object { const val MAX = 100 fun printMax() = println(MAX) } } uses const val but no @JvmStatic on function.
      4. Final Answer:

        The code with @JvmField on MAX and @JvmStatic on printMax() -> Option A
      5. Quick Check:

        @JvmField for fields, @JvmStatic for static methods [OK]
      Hint: Use @JvmField for fields and @JvmStatic for methods [OK]
      Common Mistakes:
      • Swapping @JvmField and @JvmStatic annotations
      • Omitting annotations and expecting static access
      • Using const val without @JvmStatic on functions
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard