Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Kotlin Dictionary
  3. @JvmStatic / @JvmField

@JvmStatic / @JvmField

Kotlin provides annotations such as @JvmStatic and @JvmField to enable smooth interoperability between Kotlin and Java. When calling Kotlin code from Java, these annotations let you use Kotlin companion objects and functions in a natural Java style.

Syntax

class MyClass {
    companion object {
        @JvmStatic
        fun staticMethod() { }   // Callable from Java as MyClass.staticMethod()

        @JvmField
        val CONSTANT = "value"   // Directly accessible from Java as MyClass.CONSTANT
    }

    @JvmOverloads
    fun greet(name: String = "World", greeting: String = "Hello") {
        println("$greeting, $name!")
    }
}

Annotation List

AnnotationDescription
@JvmStaticGenerates a companion object method as a Java static method.
@JvmFieldExposes a property as a Java field for direct access. No getter/setter is generated.
@JvmOverloadsGenerates overloaded methods from a function with default parameters.
@Throws(Exception::class)Declares a Java checked exception. Allows Java code to catch it.
@JvmName("name")Changes the method or class name in the generated bytecode.

Sample Code

import java.io.IOException

class DatabaseHelper {
    companion object {
        // With @JvmStatic, Java can call this as DatabaseHelper.create().
        @JvmStatic
        fun create(url: String): DatabaseHelper {
            println("Connecting: $url")
            return DatabaseHelper()
        }

        // With @JvmField, the field is directly accessible from Java.
        @JvmField
        val DEFAULT_URL = "jdbc:sqlite:local.db"
    }

    // With @JvmOverloads, overloaded methods are generated for default parameters.
    @JvmOverloads
    fun query(sql: String, limit: Int = 100, offset: Int = 0): List<String> {
        println("Query: $sql (limit=$limit, offset=$offset)")
        return listOf("row1", "row2")
    }

    // With @Throws, the exception can be caught on the Java side.
    @Throws(IOException::class)
    fun readFile(path: String): String {
        // Operation that may throw IOException
        return "file content"
    }
}

fun main() {
    // @JvmStatic allows calling without going through Companion.
    val db = DatabaseHelper.create(DatabaseHelper.DEFAULT_URL)

    // Call with default parameters omitted.
    db.query("SELECT * FROM users")
    db.query("SELECT * FROM products", 50)
    db.query("SELECT * FROM orders", 10, 20)

    // Exception declared with @Throws
    val content = db.readFile("/path/to/file")
    println(content)
}

Overview

Kotlin has full Java interoperability, but by default, calling members of a Kotlin companion object from Java requires syntax like MyClass.Companion.method(). Adding @JvmStatic lets you call it with the natural Java syntax MyClass.method().

JVM annotations are not needed in Kotlin-only projects, but use them when you need to integrate with Android Java code or existing Java libraries.

For details on object declarations, see object / companion object.

If you find any errors or copyright issues, please .