Lesson · Built-in Annotations
Built-in Annotations
11 min readBuilt-in Annotations
An annotation attaches metadata to a declaration and changes nothing until something reads it. The annotations the JDK ships each name a claim, and every claim has a reader: javac, the serialization machinery, Javadoc, a tool. Learn the seven you meet daily by asking who checks them, and what silence costs when the claim is wrong.
Metadata, not code
An annotation attaches metadata to a declaration: a line before a class, method, field, parameter or type, carrying values and nothing else. It is not code that runs, it is not spliced into a method body, and the JVM does not act on it. Whatever happens next depends on a reader: the compiler, a tool, or a library that scans it.
Every built-in annotation in this lesson works the same way. Somebody wrote down a claim (this method overrides a supertype method, do not use this API, this array is never written to), and a specific consumer checks it. When the claim holds, the annotation is documentation with teeth. When it does not, you have written a comment the machine believes.
An annotation is a claim; the value is in whoever checks it.
Seven annotations, seven claims
Here is the set you meet in ordinary code, with the reader that makes each one worth writing. The last column is the interesting one: an annotation whose claim is wrong, or missing, does not announce itself.
| Annotation | The claim | Who checks it | Without it |
|---|---|---|---|
| @Override | This method overrides or implements a declaration from a supertype. | javac, while compiling | A changed parameter list becomes an overload: the parent method still runs and yours is never called. |
| @Deprecated | This API is discouraged; forRemoval adds that it will be removed. | javac warns at each use, IDEs show strikethrough, Javadoc lists it | Nothing warns anyone. The @deprecated Javadoc tag alone does not move the compiler. |
| @SuppressWarnings | The named warning categories are accounted for on this element and everything inside it. | javac, and IDEs, at compile time | The warning is reported. When you do suppress, the name must be a real category: unchecked, deprecation, removal, preview, or a documented -Xlint key. Unknown names are silently ignored. |
| @FunctionalInterface | This interface has exactly one abstract method. | javac, while compiling | A second abstract method slips in, and the failure appears at every lambda call site instead of at the declaration. |
| @SafeVarargs | This varargs method only reads its array and never lets it escape. | javac accepts the promise and drops unchecked warnings at call sites | The unchecked warnings stay, and the pollution the promise denied surfaces later as a ClassCastException. |
| @Serial | This field or method plays a defined role in serialization. | javac checks the signature (JDK 14 and later) | A typo such as readObjct is just an unused private method; deserialization ignores it in silence. |
| @Native | Native code references this constant. | javac, when it writes native headers with the -h option | The constant never reaches the generated header, and the native side cannot see it. |
- Five of them are read by javac: @Override, @FunctionalInterface, @SafeVarargs and @Serial make it check the declaration, and @Native feeds the native header it writes. Five different jobs, one shape.
- Two of them move warnings rather than fail builds: @Deprecated and @SuppressWarnings are policy, not proof. They cannot catch a wrong claim, only pass it along or mute it.
- Version notes worth knowing: @Deprecated gained since and forRemoval in Java 9, @SafeVarargs accepted private instance methods from Java 9, and @Serial arrived in JDK 14.
- Neighbours own the deep end: the functional interfaces lesson takes @FunctionalInterface and the SAM rules, and the serialization lesson takes the protocol that @Serial watches over. Here we only need the claims.
Read the table right to left: pick the annotation by the reader you want (a build failure, a warning, a header file), not by the declaration you want to decorate.
Built-in annotations · one claim each
pick an annotation@Override String describe() { ... }
the claim
This method overrides or implements a method declared in a supertype.
enforced by
javac, while compiling: the build fails if the method overrides nothing and matches no public method of Object.
if the claim is wrong or missing
A parameter list that drifted by one type becomes a new overload. The parent method keeps running, your method is never dispatched to, and the compiler says nothing.
@Deprecated: a warning you choose to give
Deprecation is the one built-in annotation whose whole job is to create friction for callers. javac warns when a deprecated element is used or overridden in code that is not itself deprecated, which is why a migration in progress stays quiet inside the old class and noisy everywhere else.
Since Java 9 the annotation carries two elements. since records the version where deprecation began, and forRemoval is a boolean that says the API is going away rather than merely being discouraged. forRemoval moves the diagnostic from the deprecation category to the removal category, so the acknowledgement to write is @SuppressWarnings("removal").
- javac warns at the use and at the override, never at the declaration: the deprecated declaration itself compiles silently.
- Use from inside a deprecated declaration is not reported, because that code is already on the way out.
- forRemoval = true upgrades the message to marked for removal and moves it to the removal warning category.
- One suppression acknowledges one use site: @SuppressWarnings("deprecation") or ("removal") on the narrowest declaration that produces the warning.
- The @deprecated Javadoc tag is separate documentation. javac even has a dep-ann category that warns when the tag appears without the annotation.
@Deprecated removes nothing. It buys a conversation with every caller, one warning at a time.
@SuppressWarnings: the one that removes checks
Suppression is a promise in the other direction: not this is true, but I have seen this warning and I am handling it. javac reads it at compile time only (SOURCE retention) and applies it to the annotated element and to everything inside it, and the nested suppressions union together.
- The four categories every compiler must know: unchecked, deprecation, removal and preview. Everything else (rawtypes, serial, fallthrough, and the rest) is implementation help that javac documents under --help-lint and accepts by name.
- Unknown names are ignored, not errors: javac silently drops a string it does not recognise, so a typo suppresses nothing while looking deliberate. "all" is not a javac category, even though blanket switches exist in other tools.
- Scope is the whole decision: on a single local variable or method, the suppression covers exactly the code that produced the warning; at class level it covers every line written in that class from now on, including code nobody has written yet.
- Some warnings cannot be suppressed at all: javac marks categories such as path and processing as not suppressible by the annotation, however loudly you ask.
A suppression is a decision you sign. Keep it next to the fix it belongs to, not next to the class declaration.
The annotations that hand javac a promise
Four annotations exist to give javac something it cannot verify on its own. Each one turns an intention into a check, and each one is trusted rather than proved.
- @FunctionalInterface turns exactly one abstract method into a requirement. Default and static methods do not count, and neither does an abstract method that redeclares a public method of Object. Annotating a type that is not a functional interface is a compile error. The functional interfaces lesson owns the full definition; here it is the marker that makes the shape explicit.
- @SafeVarargs claims that the varargs array is only read, never written to and never handed out for someone else to write to. It is legal only on a static, final or private method (private instance methods since Java 9) and on constructors, and javac then stops issuing unchecked warnings at the call sites. A false promise compiles fine, and the ClassCastException lands somewhere else entirely.
- @Serial has been available since JDK 14 and marks one of the seven serialization members: writeObject, readObject, readObjectNoData, writeReplace, readResolve, serialVersionUID and serialPersistentFields. javac checks that the signature really is one of those roles. The protocol those members serve belongs to the serialization lesson.
- @Native is the niche one: a SOURCE retention, field-only marker for a constant that native code references. javac uses it when it writes native headers with -h, so a class with native methods and annotated constants produces declarations the C side can include.
None of these annotations performs the behaviour it claims: @SafeVarargs does not make memory safe, and @Serial does not serialize anything.
Built-ins · who reads what
a claim with no reader is a comment
the annotations
Each one records a claim at the declaration. Whether the claim is enforced depends only on who reads it, and how far the annotation travels is the retention decision covered next.
compile time
Reads every one of them from the declaration: checks overrides and functional shapes, moves deprecation and suppression warnings, validates serialization members, and writes native headers.
reflection and serialization
These three keep RUNTIME retention, so they survive into the class file and any scanner can read them; nothing in the JDK acts on them there. The serialization runtime reads the members @Serial names, not the annotation itself.
IDEs, Javadoc, static analysis
Strikethrough for deprecated APIs, deprecation lists in generated docs, inspections that suggest a missing override, and editors that flag a serialization member with the wrong signature.
Reviewing an annotation in a pull request
When an annotation crosses your desk, four questions reveal whether it is doing work or merely decorating:
- What is the claim, in one sentence? If nobody can write it down, the annotation is a mood.
- Who reads it? A compiler check, a warning, a processor, a runtime scan, or nobody at all.
- What happens if the claim is false? If the answer is nothing until production, treat the annotation like a comment: fine to keep, dangerous to trust.
- Is the scope right? The narrowest declaration for suppressions and promises; a whole type only for markers that genuinely describe the whole type.
An annotation you cannot name a reader for is a comment with an at sign.
The class level @SuppressWarnings("all") that hides the future
Two problems meet in one line. javac does not define "all" as a category: it knows the mandated names (unchecked, deprecation, removal, preview) and its documented -Xlint keys, and it silently ignores a name it does not recognise, so this annotation suppresses nothing while telling every reader that the warnings were considered. In tools that do define a blanket switch, the class level placement causes the opposite damage: every warning in the file goes quiet, including the ones written next month by someone who never saw this line, and the first failure lands in production with no paper trail. Name the category, and put the suppression on the narrowest declaration that produces it.
Interview angles
Is an annotation code that runs?+
No. An annotation is metadata on a declaration, and the JVM does not act on it. Observable behaviour comes from a consumer: javac, a processor, or a framework that scans it through reflection.
The one place runtime life appears is reflection: annotations with RUNTIME retention are in the class file, and the instance you read is a JDK proxy whose element methods return stored values. Nothing happens until some code asks for it.
What does @Deprecated actually change?+
It makes javac warn at each use and override outside deprecated code, and it is what IDEs and Javadoc key off. since records the version, and forRemoval = true moves the diagnostic to the removal category.
Common misconception: That the @deprecated Javadoc tag is equivalent. The tag is documentation for readers; javac even has a dep-ann category that warns when the tag appears without the annotation.
When may you use @SafeVarargs?+
Only on a static, final or private method, or on a constructor, and only when the declaration really has a varargs parameter. Private instance methods became legal in Java 9.
The annotation is an assertion about the body: the array is never written to and never escapes. javac cannot verify it, so a wrong assertion is paid later at a call site, as a ClassCastException far from the annotated method.
Follow-up: What does the compiler warn about when the annotation is missing? Possible heap pollution from a parameterized vararg type, reported by the varargs lint category.
Why write @Override when the compiler does not require it?+
It converts intent into a compile error: a typo or a changed parameter list stops being a silent overload and becomes a build failure.
The inheritance and overloading lessons already made this case at length; in one line, without the annotation the old method stays in charge and the new one is never dispatched to.