Java Annotations Explained for Beginners | Built-in & Custom Annotations in Java

Author: Ritika
0

 

When I first saw @Override in Java, I honestly thought it was some kind of fancy shortcut or maybe a special keyword. Many students get confused here. We think annotations are complicated or “advanced Java stuff.” But trust me, once you understand the idea, it’s actually very simple.

 

In this blog, I’ll explain Java Annotations in simple words — the way I wish someone had explained to me in 2nd year when I was preparing for exams and building mini projects.

 

Let’s start from the basics.

 

Java Annotations Explained for Beginners | Built-in & Custom Annotations in Java
(toc) #title=(Table of Content)

 

What Are Java Annotations?

In simple words, annotations are metadata in Java.

 

Now you might ask, what is metadata?

Basically metadata means data about data. In Java annotations give extra information about classes, methods, variables or other elements. They don’t directly change the program logic but they provide instructions to the compiler or runtime.

 

Think of it like writing a small note on your assignment:

Sir, please check page 3 carefully.

That note doesn’t change your assignment content. But it gives extra information to the teacher. Annotations work in a similar way.

 

Annotations start with @.

 

Example:

@Override
public String toString() {
    return "Student";
}

 

Here, @Override is an annotation.

 

Built-in Annotations in Java

Java provides some predefined annotations. These are called built-in annotations. You must have already used some of them without fully understanding.

 

Let’s discuss the important ones.

 

1. @Override

This is the most common annotation students use.

It tells the compiler that a method is overriding a method from the parent class.

 

Example:

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Bark");
    }
}

 

Why is this useful?

If you accidentally make a mistake like:

void Sound() { }

 

(Notice the capital S)

Without @Override, the compiler won’t complain. But with @Override, it will show an error.

 

When I learned this, I realized it saves us from silly mistakes during exams and project work.

 

2. @Deprecated

This annotation indicates that a method or class is outdated and should not be used.

 

Example:

@Deprecated
void oldMethod() {
    System.out.println("This is old method");
}

 

If someone uses this method, the compiler gives a warning.

 

It’s like when seniors say:

“Don’t use that old lab manual. Use the updated one.”

Same concept.

 

3. @SuppressWarnings

This annotation tells the compiler to ignore specific warnings.

 

Example:

@SuppressWarnings("unchecked")
public void test() {
    List list = new ArrayList();
}

 

Sometimes during coding practice, especially with collections, we get warnings. Instead of seeing too many warnings, we can suppress them.

 

But be careful — don’t misuse it just to hide problems.

 

4. @FunctionalInterface

This annotation is used for functional interfaces (used in Lambda expressions).

 

Example:

@FunctionalInterface
interface MyInterface {
    void show();
}

 

If you add more than one abstract method, the compiler will show an error.

This helps maintain clean code, especially in modern Java (Java 8+).

 

Summary of Built-in Annotations

Annotation Purpose When to Use
@Override Ensures method overriding When overriding parent method
@Deprecated Marks old code When method/class should not be used
@SuppressWarnings Ignores warnings When specific warnings are unnecessary
@FunctionalInterface Ensures single abstract method For lambda expressions

 

Custom Annotations in Java

Now comes the interesting part — creating your own annotation.

When I first learned this in Advanced Java, I thought this is only used in frameworks like Spring. But actually, we can create simple custom annotations for understanding.

 

How to Create a Custom Annotation?

We use @interface keyword.

 

Example:

@interface MyAnnotation {
    String value();
}

 

This creates a custom annotation named MyAnnotation.

 

We can use it like this:

@MyAnnotation(value = "Hello")
class Demo { }

 

It looks simple, but there are some important concepts.

 

Important Meta-Annotations

Meta-annotations are annotations used on annotations.

Yes, sounds confusing. But stay with me.

 

Java provides:

  • @Retention
  • @Target
  • @Documented
  • @Inherited

 

1. @Retention

Defines how long the annotation is available.

@Retention(RetentionPolicy.RUNTIME)

 

There are three types:

  • SOURCE → Available only in source code
  • CLASS → Available in class file
  • RUNTIME → Available during runtime (used with reflection)

If you’re preparing for exams, remember this properly. It’s a common question.

 

2. @Target

Defines where annotation can be applied.

 

Example:

@Target(ElementType.METHOD)

 

It can be used on:

  • METHOD
  • CLASS
  • FIELD
  • CONSTRUCTOR
  • etc.

So basically, it restricts usage.

 

Complete Example of Custom Annotation

Let’s make a simple example like a mini college project.

 

Suppose we want to create an annotation to mark important methods.

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface ImportantMethod {
    String level() default "HIGH";
}

 

Using it:

class Student {

    @ImportantMethod(level = "MEDIUM")
    public void prepareForExam() {
        System.out.println("Study regularly");
    }
}

 

This doesn’t change logic automatically. But we can use reflection to read it.

 

Many frameworks like Spring Boot use annotations heavily. For example:

  • @RestController
  • @Autowired
  • @Component

These are custom annotations internally.

 

Usage Examples in Real Projects

Let me share something from my own learning experience.

 

When I built an Employee Management System using Spring Boot, I saw:

@RestController
@RequestMapping("/api")

 

At that time, I didn’t fully understand. But later I realized:

Annotations tell Spring how to handle classes and methods.

 

Some real-life usage examples:

 

1. In Web Applications

  • @Controller
  • @GetMapping
  • @PostMapping

These define routes and request handling.

 

2. In Testing

@Test in JUnit

@Test
void testAddition() { }

 

This tells JUnit that this is a test method.

 

3. In Validation

  • @NotNull
  • @Size

Used in form validation.

 

Basically, annotations reduce boilerplate code.

Instead of writing 50 lines of configuration, we write one simple annotation.

 

Advantages of Java Annotations

  • Cleaner Code
    Instead of writing XML configuration (like older Java projects), annotations make code simple and readable.
  • Compile-Time Checking
    Annotations like @Override help catch mistakes early.
  • Better Documentation
    Annotations act like notes for developers.
  • Used in Modern Frameworks
    Spring, Hibernate, JUnit — all heavily depend on annotations.
  • Reduces Repetitive Code
    Instead of manually configuring everything, annotations automate many tasks.

 

Common Confusion Students Have

Many students think:

“Do annotations execute code?”

Answer: No.

They provide information. The compiler or frameworks use that information.

 

Another confusion:

“Are annotations mandatory?”

No. But in modern Java development, they are very important.

 

Conclusion

So let’s quickly revise.

  • Java Annotations are metadata.
  • They start with @.
  • Built in annotations like @Override help to avoid mistakes.
  • Custom annotations can be created using @interface.
  • Frameworks like Spring use annotations frequently.
  • They make the code cleaner and easier to manage.

 

When I first studied annotations it felt theoretical. But once I started using Spring Boot, everything made sense.

If you are a beginner then don’t worry. Start with built in annotations. Practice small examples. Then slowly explore custom annotations.

Trust me, once you understand annotations Java development becomes much smoother.

 

Frequently Asked Questions (FAQs)

What are Java Annotations ?

Java Annotations are special markers that provide extra information about classes or methods. They don’t change logic directly but help the compiler or frameworks.

What is the use of @Override annotation?

@Override ensures that a method is correctly overriding a parent class method. It helps catch mistakes at compile time.

What is the difference between built-in and custom annotations?

Built-in annotations are provided by Java (like @Deprecated). Custom annotations are created by developers using @interface.

Do annotations affect performance?

Not directly. They don’t execute code. But frameworks use them to manage behavior efficiently.

Are annotations important for interviews?

Yes, especially if you are preparing for Java Developer or Spring Boot roles. Interviewers often ask about @Override, @Retention, and real-world usage.

 

If you’re preparing for exams or building projects, make sure you practice annotations practically. Don’t just memorize definitions. Try small examples.

 

Happy coding 🙂

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !)#days=(20)

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. By clicking "Accept", you consent to our use of cookies. Privacy Policy
Accept !
Breaking News | News |