Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Annotations Explained: Types, Placement, and Practical Examples

Java Annotations Explained

Discover what Java annotations are, how they’re categorized, and how to apply them effectively with clear, real‑world examples.

Annotations are metadata embedded in source code that convey additional information to the compiler and, when needed, to runtime systems. They do not become part of the executed bytecode, but they can influence compilation, code generation, and reflection.

Annotations always begin with the @ symbol. The most common syntax is:

@AnnotationName

The @Override Annotation in Action

The @Override annotation signals that a method is intended to override a superclass method. Although not mandatory, using it allows the compiler to flag mismatches such as incorrect signatures.

Example 1: @Override Annotation

class Animal {
  public void displayInfo() {
    System.out.println("I am an animal.");
  }
}

class Dog extends Animal {
  @Override
  public void displayInfo() {
    System.out.println("I am a dog.");
  }
}

class Main {
  public static void main(String[] args) {
    Dog d1 = new Dog();
    d1.displayInfo();
  }
}

Output

I am a dog.

In this example, displayInfo() exists in both Animal and Dog. The overridden method in Dog is executed, demonstrating polymorphism.


Annotation Formats

Annotations can include members (attributes). They are grouped into three main categories:

1. Marker Annotations

These contain no elements and are used solely as flags. Parentheses are optional.

@AnnotationName

2. Single‑Element Annotations

Contain one element, conventionally named value. When only one element is present, its name may be omitted.

@AnnotationName("elementValue")

3. Multi‑Element Annotations

Include several elements, separated by commas.

@AnnotationName(element1 = "value1", element2 = "value2")

Where to Place Annotations

Annotations can precede any declaration: classes, methods, fields, interfaces, and more. Since Java 8, they can also annotate types.

1. Above Declarations

Typical placement: directly above the target declaration.

Example 2: @SuppressWarnings Annotation

import java.util.*;

class Main {
  @SuppressWarnings("unchecked")
  static void wordsList() {
    ArrayList wordList = new ArrayList<>();
    // This causes an unchecked warning
    wordList.add("programiz");
    System.out.println("Word list => " + wordList);
  }

  public static void main(String args[]) {
    wordsList();
  }
}

Output

Word list => [programiz]

Without @SuppressWarnings("unchecked"), the compiler would emit a warning about unchecked operations. The example also demonstrates how generics mitigate this warning:

ArrayList<String> wordList = new ArrayList<>();

2. Type Annotations (Java 8+)

Type annotations allow you to annotate any occurrence of a type, enhancing static analysis and null‑safety.

// Constructor invocation
new @Readonly ArrayList<>();

// Type definition
@NonNull String str;
@NonNull List<String> newList;
List<@NonNull String> newList;

// Type cast
newStr = (@NonNull String) str;

// Extends/implements
class Warning extends @Localized Message

// Throws clause
public String readMethod() throws @Localized IOException

These annotations empower tools to enforce stronger type checks and prevent runtime errors such as NullPointerException.


Types of Annotations

  1. Predefined Annotations
    • @Deprecated
    • @Override
    • @SuppressWarnings
    • @SafeVarargs
    • @FunctionalInterface
  2. Meta‑Annotations
    • @Retention
    • @Documented
    • @Target
    • @Inherited
    • @Repeatable
  3. Custom Annotations

    Define domain‑specific annotations for your projects. Refer to the Java Annotation Types tutorial for guidance.


Practical Uses of Annotations


Java

  1. Master Java Operators: Types, Syntax, & Practical Examples
  2. Java Comments: Types, Usage, and Best Practices
  3. Mastering Java if…else: Control Flow Explained
  4. Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
  5. Java Break Statement: How, When, and Labeled Breaks Explained
  6. Mastering Java Arrays: Declaration, Initialization, and Manipulation
  7. Java Methods: How to Define, Call, and Use Them Effectively
  8. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  9. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  10. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained