0
0
Kotlinprogramming~20 mins

Kotlin annotations for Java callers (@JvmStatic, @JvmField) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of @JvmStatic annotated companion object function call from Java
Consider this Kotlin code with a companion object and a function annotated with @JvmStatic. What will be the output when called from Java as Example.Companion.greet() and Example.greet()?
Kotlin
class Example {
    companion object {
        @JvmStatic
        fun greet() = "Hello from @JvmStatic"

        fun greetNormal() = "Hello from normal companion"
    }
}
AExample.Companion.greet() prints "Hello from @JvmStatic"; Example.greet() does not compile
BExample.Companion.greet() prints "Hello from normal companion"; Example.greet() prints "Hello from @JvmStatic"
CExample.Companion.greet() prints "Hello from @JvmStatic"; Example.greet() prints "Hello from @JvmStatic"
DExample.Companion.greet() does not compile; Example.greet() prints "Hello from @JvmStatic"
Attempts:
2 left
💡 Hint
Remember that @JvmStatic creates a static method in the containing class for Java callers.
Predict Output
intermediate
2:00remaining
Effect of @JvmField on Kotlin property visibility in Java
Given this Kotlin class with a property annotated with @JvmField, what is the output or behavior when accessing Example.CONSTANT from Java?
Kotlin
class Example {
    @JvmField
    val CONSTANT = "Kotlin constant"

    val normalConstant = "Normal constant"
}
AJava can access Example.CONSTANT directly as a field; Example.normalConstant requires getter method
BJava cannot access Example.CONSTANT directly; both require getter methods
CJava can access both Example.CONSTANT and Example.normalConstant directly as fields
DJava cannot access either Example.CONSTANT or Example.normalConstant
Attempts:
2 left
💡 Hint
Think about how @JvmField changes the generated bytecode for properties.
🔧 Debug
advanced
2:00remaining
Why does Java code fail to call Kotlin companion function without @JvmStatic?
This Kotlin code defines a companion object function without @JvmStatic. Java code tries to call Example.greet() but fails. Why?
Kotlin
class Example {
    companion object {
        fun greet() = "Hello"
    }
}
ABecause the function requires an instance of Example to be called
BBecause Kotlin companion objects cannot be called from Java at all
CBecause the function is private by default and not visible to Java
DBecause without @JvmStatic, the function is only inside the companion object instance, not as a static method in Example class
Attempts:
2 left
💡 Hint
Check how Kotlin companion object functions are compiled and accessed from Java.
📝 Syntax
advanced
2:00remaining
Which Kotlin property declaration with @JvmField is valid?
Select the valid Kotlin property declaration using @JvmField that compiles without error.
A
"class Example {
    @JvmField
    var count: Int = 0
}"
B
"class Example {
    @JvmField
    private val name: String = "Test"
}"
C
"class Example {
    @JvmField
    val age: Int
}"
D
"class Example {
    @JvmField
    fun getValue() = 42
}"
Attempts:
2 left
💡 Hint
Remember @JvmField can only be applied to certain property types and visibility.
🚀 Application
expert
3:00remaining
How to expose Kotlin top-level constant as a Java static field?
You want to expose a Kotlin top-level constant so Java code can access it as a static field without calling a getter method. Which Kotlin declaration achieves this?
A
@JvmField
val MAX_COUNT = 100
Bconst val MAX_COUNT = 100
C
@JvmStatic
val MAX_COUNT = 100
Dval MAX_COUNT = 100
Attempts:
2 left
💡 Hint
Consider how Kotlin compiles const val and @JvmField at top-level.