0
0
Kotlinprogramming~10 mins

Kotlin annotations for Java callers (@JvmStatic, @JvmField) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the function callable as a static method from Java.

Kotlin
object Utils {
    @[1]
    fun greet() = "Hello from Kotlin"
}
Drag options to blanks, or click blank then click option'
AJvmField
BJvmOverloads
CJvmStatic
DJvmName
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JvmField instead of @JvmStatic for functions.
2fill in blank
medium

Complete the code to expose the property as a public field in Java.

Kotlin
class Config {
    @[1]
    val version = "1.0"
}
Drag options to blanks, or click blank then click option'
AJvmField
BJvmName
CJvmStatic
DJvmOverloads
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JvmStatic on properties instead of @JvmField.
3fill in blank
hard

Fix the error by choosing the correct annotation to allow Java static access to the companion object's function.

Kotlin
class Helper {
    companion object {
        @[1]
        fun help() = "Helping"
    }
}
Drag options to blanks, or click blank then click option'
AJvmStatic
BJvmSynthetic
CJvmName
DJvmField
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JvmField on functions inside companion objects.
4fill in blank
hard

Fill both blanks to expose a property as a public static field accessible from Java.

Kotlin
class Constants {
    companion object {
        @[1]
        @[2]
        val MAX_COUNT = 100
    }
}
Drag options to blanks, or click blank then click option'
AJvmField
BJvmStatic
CJvmName
DJvmSynthetic
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one annotation, causing no static field in Java.
5fill in blank
hard

Fill all three blanks to create a Kotlin object with a static function and a public field accessible from Java.

Kotlin
object Logger {
    @[1]
    fun log(message: String) = println(message)

    @[2]
    val DEFAULT_TAG = "APP"
}
Drag options to blanks, or click blank then click option'
AJvmStatic
BJvmField
CJvmName
DJvmSynthetic
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing @JvmField and @JvmStatic usage on functions and properties.