Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
4. Advanced Java & Frameworks
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

37. Annotations & Reflection

Metadata tagging and inspecting your own code while it's running.

Java Programming: From Zero to Enterprise
4. Advanced Java & Frameworks
February 22, 2026
64
A

[!NOTE] We touched upon Annotations briefly with @Override. But Annotations govern modern Java frameworks entirely. If you want to use Spring or Hibernate, you must understand exactly how decorators inject massive amounts of hidden logic!

What is an Annotation?

An annotation (the @ symbol followed by a name) is Metadata. It does not perform any physical logic by itself. It is simply a tag stitched onto a class or method.

@Deprecated // Warning! Do not use this method anymore!
public void oldLegacyLogin() { ... }

How Frameworks Use Annotations (Reflection)

If an annotation is just a descriptive label, how does the famous Spring Framework use @RestController to magically launch a Tomcat web server and start mapping HTTP requests?

It uses Reflection.

Java Reflection is an incredibly dangerous, ultra-powerful API that allows the Java JVM to look in the mirror. It lets code inspect itself while it is actively running.

  1. The Spring Framework starts up.
  2. It uses Reflection to aggressively scan every single Class in your entire /src directory.
  3. It asks: "Does this Class have the @RestController tag glued to it?"
  4. If Yes, Spring dynamically hijacks the class, generates a massive proxy server block in memory, injects JSON parsing logic, and opens ports to the internet!
// Without you writing ANY server code, Spring detects this tag and boots a server for it via Reflection!
@RestController
public class AuthenticationAPI {

    // Spring scans for this tag, and routes HTTP GET requests straight into your method!
    @GetMapping("/login")
    public String attemptLogin() {
        return "You have reached the API!";
    }
}

[!CAUTION] Applying Reflection directly yourself (like invoking methods via string names) is extremely slow and breaks compile-time safety. Unless you are literally building the next Spring Boot framework yourself, avoid utilizing the java.lang.reflect package in business logic!

Annotations Need Something to Read Them

An annotation is metadata. It becomes powerful only when a compiler, framework, test runner, or runtime scanner reads it and acts on it. @Override is read by the compiler. Spring annotations are read by Spring.

Custom Annotation Shape

@interface AuditLog {
    String action();
}
@AuditLog(action = "CREATE_USER")
void createUser() {
    // business logic
}

By itself this does nothing. Some code must inspect the method and react to @AuditLog.

Reflection Tradeoff

Reflection is powerful for frameworks, serializers, dependency injection, and test tools. In everyday business code, prefer normal method calls because they are safer, faster, and easier for IDEs to refactor.

Common Mistakes

  • Assuming an annotation automatically performs behavior.
  • Using reflection where a normal interface would be simpler.
  • Accessing private fields reflectively and breaking encapsulation.
  • Forgetting that reflection errors often appear at runtime, not compile time.

Mini Practice

Create a custom marker annotation called Important. Add it to two methods. Then describe what kind of framework behavior could scan for it.

Practice Lab: Annotation Thinking

Annotations are metadata, so practice designing one and explaining who would read it.

  1. Create an annotation named Important.
  2. Add it to two methods.
  3. Create a second annotation named AuditLog with an action value.
  4. Add @AuditLog(action = "CREATE_USER") to a method.
  5. Write a comment explaining what a framework could do after finding that annotation.

Goal: Understand that annotations mark code; separate tooling or framework code gives them meaning.

Revision Checkpoint

  • Annotation: Metadata attached to code.
  • Framework role: External code reads annotations and acts on them.
  • @Override: Compiler-checked annotation.
  • Reflection: Runtime inspection of classes, methods, and fields.
  • Caution: Reflection can weaken compile-time safety and performance.

Before the quiz: Explain why an annotation alone does not perform behavior.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Annotations
10 questions5 min
Lesson 7 of 11 in 4. Advanced Java & Frameworks
Previous in 4. Advanced Java & Frameworks
36. JDBC: Connecting to SQL Databases
Next in 4. Advanced Java & Frameworks
38. The JVM Garbage Collector
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories