Course contentsShow
← Java
Lesson 2435 of 2,87046. AnnotationsFree lesson

Use Cases for SOURCE Retention

Explore practical examples like @Override and @SuppressWarnings that exist only in source code.

Use Cases for SOURCE Retention

What you'll learn: You'll discover which annotations use SOURCE retention and why they only need to exist during compilation, not at runtime.

Why Annotations That Disappear Make Sense

Some annotations exist solely to help the compiler do its job—they're like construction scaffolding that gets removed once the building is complete. Annotations with SOURCE retention are discarded after compilation, leaving no trace in the bytecode or at runtime.

Practical Examples You Already Know

@Override

This is the classic SOURCE retention example. The compiler checks that you're actually overriding a superclass or interface method, then throws the annotation away. Once the .class file is created, @Override has served its purpose—no framework or reflection code needs to see it.

@Override
public String toString() {
    return "example"; // Compiler verified, annotation now gone
}

@SuppressWarnings

Similarly, @SuppressWarnings tells the compiler to silence specific warnings. After compilation, there's no reason for this annotation to stick around—it was a message to the compiler, not to the JVM or runtime libraries.

@SuppressWarnings("unchecked")
public void processRawList(List list) {
    // Compiler ignored the warning, annotation discarded
}

The Analogy

Think of SOURCE retention like sticky notes left for your editor during document review. Once the final version is published, you peel off the notes—readers don't need to see "check this spelling" or "verify this fact." The notes guided production but don't belong in the finished product.

When to Use SOURCE Retention

Choose SOURCE retention when your annotation:

  • Guides compilation or static analysis
  • Prevents coding mistakes (like @Override)
  • Controls compiler behavior (like @SuppressWarnings)
  • Has no runtime purpose whatsoever

Key Takeaway: SOURCE retention is perfect for compiler-oriented annotations like @Override and @SuppressWarnings that serve their purpose during code compilation and don't need to survive into bytecode or runtime.