Deep Kotlin tutorial covering @JvmStatic, @JvmField, @JvmOverloads, annotations with examples, diagram, mistakes, and interview answers.

JVM Annotations, KAPT, KSP, and Reflection

What is JVM Annotations, KAPT, KSP, and Reflection?

JVM Annotations, KAPT, KSP, and Reflection is an important Kotlin topic from 1.7 JVM Interop and Annotations. In this lesson we focus only on the concepts that belong together: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand JVM Annotations, KAPT, KSP, and Reflection in simple language
  • Use @JvmStatic in real Kotlin or Android code
  • Explain JVM Annotations, KAPT, KSP, and Reflection in interviews with examples and trade-offs

How it works

JVM Annotations, KAPT, KSP, and Reflection: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection works at the boundary between Kotlin and Java/JVM tooling. The goal is to keep generated code, Java callers, nullability, and runtime reflection understandable so Kotlin code remains safe and testable.

  1. Identify what problem JVM Annotations, KAPT, KSP, and Reflection: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • @JvmStatic: core concept in this lesson
  • @JvmField: core concept in this lesson
  • @JvmOverloads: core concept in this lesson
  • annotations: core concept in this lesson
  • KAPT: core concept in this lesson
  • KSP: core concept in this lesson
  • reflection: core concept in this lesson
  • Java caller support

Important differences

Concept 1Concept 2How to remember it
KAPTKSPKAPT uses Java stubs; KSP reads Kotlin symbols.
Generated codeReflectionGenerated code is compile-time; reflection inspects at runtime.
Kotlin APIJava APIKotlin can use defaults directly; Java may need overloads.

Diagram

flowchart TD
A["1.7 JVM Interop and Annotations"] --> B["JVM Annotations, KAPT, KSP, and Reflection"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Java-friendly Kotlin API

class Logger {
@JvmOverloads
fun log(message: String, tag: String = "SkillUp") {
println("$tag: $message")
}
}

Checked exception for Java

@Throws(IOException::class)
fun readConfig(): String = file.readText()

When to use it

Use it when: Use this when Kotlin code has to call into an existing Java library, generate code at build time, or share business logic across Android, iOS, and backend targets.

Avoid it when: Avoid leaking platform-specific APIs, such as Android's Context, into shared/common code - keep the boundary explicit.

Common mistakes

  • Ignoring platform type nullability
  • Using reflection in hot paths
  • Adding annotation processors without checking build impact
  • Forgetting Java callers when designing public APIs
  • Mixing KAPT and KSP without a reason

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is JVM Annotations, KAPT, KSP, and Reflection in 1.7 JVM Interop and Annotations?

Answer: JVM Annotations, KAPT, KSP, and Reflection is an important Kotlin topic from 1.7 JVM Interop and Annotations. In this lesson we focus only on the concepts that belong together: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

class Logger {
@JvmOverloads
fun log(message: String, tag: String = "SkillUp") {
println("$tag: $message")
}
}
  • Trade-off: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.
  • Common mistake: Ignoring platform type nullability

2. How does JVM Annotations, KAPT, KSP, and Reflection work in Kotlin or Android code?

Answer: JVM Annotations, KAPT, KSP, and Reflection: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection works at the boundary between Kotlin and Java/JVM tooling. The goal is to keep generated code, Java callers, nullability, and runtime reflection understandable so Kotlin code remains safe and testable.

@Throws(IOException::class)
fun readConfig(): String = file.readText()
  • Trade-off: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.
  • Common mistake: Using reflection in hot paths

3. What is one common mistake with JVM Annotations, KAPT, KSP, and Reflection?

Answer: A common mistake is: Ignoring platform type nullability Another is: Using reflection in hot paths

class Logger {
@JvmOverloads
fun log(message: String, tag: String = "SkillUp") {
println("$tag: $message")
}
}
  • Trade-off: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.
  • Common mistake: Ignoring platform type nullability

4. How would you explain JVM Annotations, KAPT, KSP, and Reflection with a small example?

Answer: JVM Annotations, KAPT, KSP, and Reflection is an important Kotlin topic from 1.7 JVM Interop and Annotations. In this lesson we focus only on the concepts that belong together: @JvmStatic, @JvmField, @JvmOverloads, annotations, KAPT, KSP, reflection. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

class Logger {
@JvmOverloads
fun log(message: String, tag: String = "SkillUp") {
println("$tag: $message")
}
}
  • Trade-off: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.
  • Common mistake: Using reflection in hot paths

5. What is the trade-off of using JVM Annotations, KAPT, KSP, and Reflection?

Answer: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.

@Throws(IOException::class)
fun readConfig(): String = file.readText()
  • Trade-off: KAPT and KSP solve related but different problems: KAPT uses Java stubs; KSP reads Kotlin symbols.
  • Common mistake: Ignoring platform type nullability

Practice task

Write one Kotlin example for JVM Annotations, KAPT, KSP, and Reflection. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.