Using Annotations
Annotate code.
Using Annotations is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Annotations?
Annotations attach metadata to code. They do not change behavior by themselves, but tools, compilers, and libraries read them to make decisions.
You write an annotation with @ followed by its name, placed right before the element it applies to.
A First Annotation
The standard library ships several useful annotations. @Deprecated marks code that should no longer be used and shows a warning at the call site.
@Deprecated("Use newGreet() instead")
fun greet() = "Hi"
fun newGreet() = "Hello"
fun main() {
println(newGreet())
}Annotations With Arguments
Annotations can take arguments, written like constructor parameters. @Deprecated accepts a message and optionally a ReplaceWith hint.
@Deprecated(
message = "Renamed for clarity",
replaceWith = ReplaceWith("sum(a, b)")
)
fun add(a: Int, b: Int) = a + b
fun sum(a: Int, b: Int) = a + b
fun main() {
println(sum(2, 3))
}Suppressing Warnings
@Suppress tells the compiler to silence specific warnings for the annotated element. You pass the warning names as strings.
@Suppress("UNUSED_VARIABLE")
fun main() {
val unused = 42
println("Runs without the unused-variable warning")
}JvmStatic and Friends
Some annotations affect how Kotlin compiles to the JVM. @JvmStatic, @JvmField, and @JvmName control the bytecode so Java callers see friendlier APIs.
These are no-ops for pure Kotlin code but matter for interop.
class Math {
companion object {
@JvmStatic
fun square(x: Int) = x * x
}
}
fun main() {
println(Math.square(5))
}Annotation Use-Site Targets
A property can generate several JVM elements: a field, a getter, a setter, and constructor parameters. Use-site targets say where an annotation should land.
Syntax: @get:, @set:, @field:, @param:.
class User(@get:JvmName("fetchName") val name: String)
fun main() {
val u = User("Ada")
println(u.name)
}Throws for Java Interop
Kotlin has no checked exceptions, so Java callers never see throws clauses unless you add @Throws. It records the exception type into the bytecode signature.
import java.io.IOException
@Throws(IOException::class)
fun readData(): String {
return "data"
}
fun main() {
println(readData())
}Multiple Annotations
You can stack several annotations on one element. Each goes on its own line (or separated by spaces). Order does not matter.
@Deprecated("old")
@Suppress("DEPRECATION")
fun legacy() = "still here"
fun main() {
println(legacy())
}Annotations on Expressions
Annotations can also apply to expressions and local declarations, not just top-level functions or classes. This is common with @Suppress inside a function body.
fun main() {
@Suppress("UNUSED_VARIABLE")
val temp = computeOnce()
println("Done")
}
fun computeOnce() = 99OptIn and Experimental APIs
Libraries mark unstable APIs with custom opt-in annotations. To use them you acknowledge the risk with @OptIn(SomeApi::class) or by propagating the annotation.
This keeps experimental features explicit rather than accidental.
fun main() {
// @OptIn would appear here when calling an experimental function
println("Opt-in keeps unstable APIs visible")
}Why Annotate?
Annotations let you communicate intent to tools: compilers warn on deprecated calls, serialization libraries map fields, test frameworks find test methods, and DI frameworks wire dependencies.
- Metadata, not logic
- Read at compile time or runtime
- Keeps configuration close to code
Quick Check
Test your understanding of using annotations.
Recap
You learned to use built-in annotations:
@Deprecated,@Suppress,@Throws- JVM interop:
@JvmStatic,@JvmName - Use-site targets like
@get:and@field: - Opt-in for experimental APIs
Next you will define your own annotations.
Frequently asked questions
Is the “Using Annotations” lesson free?
Yes — the full text of “Using Annotations” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Using Annotations”?
Annotate code. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using Annotations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using Annotations
- Defining Annotations
- Reflection Basics
- Practical Reflection