0
0
Kotlinprogramming~5 mins

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

Choose your learning style9 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.