Challenge - 5 Problems
Kotlin Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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" } }
Attempts:
2 left
💡 Hint
Remember that @JvmStatic creates a static method in the containing class for Java callers.
✗ Incorrect
The @JvmStatic annotation generates a static method in the containing class, so Java can call Example.greet() directly. The companion object still has the instance method, so Example.Companion.greet() also works and returns the same string.
❓ Predict Output
intermediate2: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" }
Attempts:
2 left
💡 Hint
Think about how @JvmField changes the generated bytecode for properties.
✗ Incorrect
The @JvmField annotation exposes the Kotlin property as a public field in Java, so Java code can access Example.CONSTANT directly. The normal property generates a private field with a public getter, so Java must use the getter method.
🔧 Debug
advanced2: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" } }
Attempts:
2 left
💡 Hint
Check how Kotlin companion object functions are compiled and accessed from Java.
✗ Incorrect
Without @JvmStatic, the function is compiled as an instance method inside the companion object class. Java must call Example.Companion.greet(), not Example.greet(). The latter does not exist as a static method.
📝 Syntax
advanced2:00remaining
Which Kotlin property declaration with @JvmField is valid?
Select the valid Kotlin property declaration using
@JvmField that compiles without error.Attempts:
2 left
💡 Hint
Remember @JvmField can only be applied to certain property types and visibility.
✗ Incorrect
@JvmField can only be applied to public properties (val or var) without custom getters/setters. Option A is valid: public var with initializer. Option A is invalid because property is private. Option A is invalid because val without initializer or getter is not allowed. Option A is invalid because @JvmField cannot be applied to functions.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Consider how Kotlin compiles const val and @JvmField at top-level.
✗ Incorrect
const val creates a compile-time constant that is exposed as a static final field in the generated Java class, accessible directly from Java. @JvmField cannot be applied to top-level properties. @JvmStatic applies only to members inside objects or companion objects. Plain val generates a private field with getter.