How do you call a Java static method and read a Java static field (like Integer.parseInt or Integer.MAX_VALUE) from Kotlin?
answer
- Class.method() and Class.FIELD — same as Java
- Kotlin has no `static`; statics are consumed via class name
- import java.lang.Integer.parseInt for unqualified use
- Reverse direction needs @JvmStatic, not this direction
basics
~10 sYou call them directly on the class name, just like in Java: Integer.parseInt("42") and Integer.MAX_VALUE. There is no special import or wrapper needed.
solid answer
~30 sKotlin has no `static` keyword, but it can consume Java statics directly through the class name. A static method is called as `JavaClass.method(args)` (e.g. `Integer.parseInt("42")`), and a static field is read as `JavaClass.FIELD` (e.g. `Integer.MAX_VALUE`, `System.out`). Kotlin does not turn these into companion-object syntax — the call site looks identical to Java. You can also `import static`-style import a member with `import java.lang.Integer.parseInt` and then call `parseInt("42")` unqualified. Note the reverse asymmetry: a Kotlin `companion object` member is NOT a real static, so calling it from Java needs `@JvmStatic` or `Companion`. But going Java → Kotlin, statics simply work.
code
kotlin · 8 linesimport java.lang.Integer.parseInt
fun demo() {
val a = Integer.parseInt("42") // qualified static method
val b = parseInt("7") // static-imported
val max = Integer.MAX_VALUE // static field
println("$a $b $max")
}go deeper
Knows you call Integer.parseInt("42") and read Integer.MAX_VALUE directly via the class name.
Adds static-style imports and explains Kotlin has no static keyword (companion object instead).
Articulates the asymmetry: consuming Java statics is transparent, but exposing Kotlin companions to Java needs @JvmStatic.
Reasons about how the JVM bytecode (invokestatic / getstatic) underlies both directions and why the language design split exists.
## The problem Kotlin deliberately has **no `static` keyword**. Instead it uses `companion object`, top-level declarations, and `object` singletons. So a natural question is: when you call *into* existing Java code that uses `static`, how do you reach those members? ## Calling Java statics from Kotlin — it just works When Kotlin consumes a Java class, static members are accessed **through the class name**, exactly like Java: ```kotlin val n: Int = Integer.parseInt("42") // static method val max: Int = Integer.MAX_VALUE // static field (a constant) System.out.println("hi") // static field System.out, then instance method val now = System.currentTimeMillis() // static method ``` There is **no companion-object syntax** on the Kotlin side — you do not write `Integer.Companion.parseInt`. The Java `static` modifier maps cleanly to a class-qualified call. ## Static imports Kotlin lets you import a static member by its fully qualified name and then use it unqualified: ```kotlin import java.lang.Integer.parseInt import java.lang.Math.max val x = parseInt("10") val y = max(3, 9) ``` This mirrors Java's `import static`, but Kotlin uses the **same `import` keyword** for everything. ## Static fields vs Kotlin constants A Java `public static final int MAX_VALUE` is read as `Integer.MAX_VALUE`. If the field is non-final, reading and (where permitted) assigning go through the same `Class.field` syntax. ## The asymmetry (don't confuse the directions) - **Java → Kotlin (this question):** statics are consumed transparently via the class name. - **Kotlin → Java (reverse):** a Kotlin `companion object` member is compiled as an *instance* method on a synthetic `Companion` object, so Java sees `MyClass.Companion.foo()` unless you annotate with `@JvmStatic`. That is a separate concern from accessing Java statics. ## Key keywords/APIs `companion object`, `object`, top-level functions, `import` (for static-style imports), `@JvmStatic` (reverse direction only).
- Why can't you write Integer.Companion.parseInt in Kotlin?Because Integer is a Java class with real JVM statics; there is no Kotlin companion object on it. Companion syntax only exists for Kotlin-defined types.
- Does Kotlin have a `static` keyword?No. It uses companion objects, top-level declarations, and object singletons instead, but it still consumes Java statics through the class name.
Reading a Java static is like dialing a company's main number printed on the door — you don't need to know which employee, you just call the class.
saying these in an interview costs you the question
- Claiming you need a wrapper or reflection to call a Java static
- Saying you must write Integer.Companion.parseInt
- Believing Kotlin has a `static` keyword
- Confusing Java→Kotlin access with the @JvmStatic reverse direction