Java Annotations

Java Annotations

Definition:

Annotations in Java provide metadata about the program that is not directly part of the code logic. They can be used to provide information to the compiler, runtime, or even used by external tools to perform specific tasks based on the metadata.
Annotations are widely used for marking classes, methods, fields, and other program elements with additional information. They are commonly seen in frameworks like Spring, Hibernate, and JUnit.

Syntax:

An annotation in Java is declared using the @ symbol followed by the annotation name. It can also include additional elements or parameters, which are passed within parentheses.
@AnnotationName(parameters)
Some commonly used annotations in Java:
        ● @Override
        ● @Deprecated
        ● @SuppressWarnings

Types of Annotations:

1. Built-in Annotations: These annotations are part of the Java language.
      ○ @Override: Indicates that a method overrides a method in the superclass.
      ○ @Deprecated: Marks a method, field, or class as outdated and signals that it should not be used.
      ○ @SuppressWarnings: Instructs the compiler to suppress specific warnings.
2. Custom Annotations: You can create your own annotations using the @interface keyword.

Example of Built-in Annotations:

@Override Example:

class Animal {
    public void sound() {
        System.out.println(“Animal makes a sound”);
    }
}

class Dog extends Animal {
    @Override
    public void sound() { // This method overrides the parent class method
        System.out.println(“Dog barks”);
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound(); // Output: Dog barks
    }
}

@Deprecated Example:

class OldClass {
    @Deprecated
    public void oldMethod() {
        System.out.println(“This method is deprecated.”);
    }
}

public class Main {
    public static void main(String[] args) {
        OldClass obj = new OldClass();
        obj.oldMethod(); // Output: This method is deprecated.
    }
}

@SuppressWarnings Example:

import java.util.ArrayList;

public class Main {
    @SuppressWarnings(“unchecked”)
    public static void main(String[] args) {
        ArrayList list = new ArrayList(); // This causes an unchecked warning
        list.add(“String”);
    }}

Custom Annotation:

You can create your own annotation using the @interface keyword.
Custom Annotation Example:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// Defining a custom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
    String value() default “default message”;
}

class DemoClass {
    @MyAnnotation(value = “Custom Annotation Example”)
    public void display() {
        System.out.println(“MyAnnotation is used.”);
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        DemoClass demo = new DemoClass();
        demo.display();

// Accessing annotation via reflection
        MyAnnotation annotation = demo.getClass()
                                                            .getMethod(“display”)
                                                            .getAnnotation(MyAnnotation.class);
        System.out.println(“Annotation value: ” + annotation.value());
    }
}

Key Points:
        ● Annotations can have default values, as shown in the custom annotation example.
        ● Annotations can have elements (like value in the custom annotation).
        ● Retention and Target meta-annotations are used to specify where and how annotations are retained.

Task:
1. Create a custom annotation @Task that takes parameters for taskName and priority. Then, write a Java class that uses this annotation.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

The training covers Java annotations, including their types, syntax, and practical applications.

Yes, it includes examples demonstrating how to use built-in annotations like @Override, @Deprecated, and @SuppressWarnings, as well as how to create custom annotations.

Yes, exercises involve applying annotations to methods and classes to understand their usage and effects.

Yes, it covers how annotations provide metadata that can be used by the compiler and at runtime to influence program behavior.

Yes, the training is self-paced and accessible online anytime.

Yes, it explains how to define custom annotations using the @interface keyword and how to apply them.

It typically takes a few hours to complete, depending on your learning pace.

Yes, this Java Annotations training is completely free on Iqra Technology Academy’s website.