Java Annotations

Master Java annotations including @Override, @FunctionalInterface, @Deprecated, @SuppressWarnings, @SafeVarargs, annotation usage, and custom annotations for the OCP 21 exam.

Table of Contents

1. Annotations Overview

Annotations provide metadata about program elements. They don't change program semantics but provide information to compilers, tools, and runtime.

1.1 Annotation Syntax

Example:
// Annotation syntax
@AnnotationName
@AnnotationName(value = "value")
@AnnotationName(param1 = "value1", param2 = "value2")

// Can be applied to classes, methods, fields, parameters, etc.
@MyAnnotation
public class MyClass {
    @MyAnnotation
    private String field;
    
    @MyAnnotation
    public void method(@MyAnnotation String param) {
    }
}

2. Built-in Annotations

2.1 @Override

Example:
class Parent {
    public void method() {
    }
}

class Child extends Parent {
    @Override
    public void method() {  // Compiler checks if actually overriding
        super.method();
    }
}

2.2 @FunctionalInterface

Example:
@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);
    
    // Can have default methods
    default void print() {
        System.out.println("Calculator");
    }
}

// Ensures interface has exactly one abstract method

2.3 @Deprecated

Example:
class Example {
    @Deprecated
    public void oldMethod() {
        // Old implementation
    }
    
    @Deprecated(since = "1.5", forRemoval = true)
    public void deprecatedMethod() {
        // Will be removed in future version
    }
    
    public void newMethod() {
        // New implementation
    }
}

2.4 @SuppressWarnings

Example:
@SuppressWarnings("unchecked")
public void method() {
    List list = new ArrayList();  // Suppresses unchecked warning
}

@SuppressWarnings({"unchecked", "deprecation"})
public void method2() {
    // Suppresses multiple warnings
}

// Common warning types: "unchecked", "deprecation", "rawtypes", "unused"

2.5 @SafeVarargs

Example:
@SafeVarargs
public final void method(String... args) {
    // Suppresses unsafe varargs warning
    // Can only be used on final or static methods
}

// Without @SafeVarargs, varargs with generic types causes warning
public void unsafeMethod(List<String>... lists) {
    // Warning: Possible heap pollution
}

3. Custom Annotations

3.1 Defining Custom Annotations

Example:
import java.lang.annotation.*;

// Marker annotation (no elements)
@interface MyAnnotation {
}

// Single-element annotation
@interface Author {
    String value();  // Special: can omit "value=" when using
}

// Multi-element annotation
@interface Book {
    String title();
    String author();
    int year() default 2024;
}

// Using annotations
@Author("John Doe")
@Book(title = "Java Guide", author = "Jane Smith", year = 2023)
class MyClass {
}

4. Annotation Retention

4.1 Retention Policies

Example:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// SOURCE - Discarded by compiler
@Retention(RetentionPolicy.SOURCE)
@interface SourceAnnotation {
}

// CLASS - Available in class file, not at runtime (default)
@Retention(RetentionPolicy.CLASS)
@interface ClassAnnotation {
}

// RUNTIME - Available at runtime via reflection
@Retention(RetentionPolicy.RUNTIME)
@interface RuntimeAnnotation {
}

// Target - specifies where annotation can be applied
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Target({ElementType.METHOD, ElementType.FIELD})
@interface MethodAndFieldAnnotation {
}

5. Exam Key Points

Critical Concepts for OCP 21 Exam:

  • @Override: Indicates method overriding, compiler checks
  • @FunctionalInterface: Ensures interface has one abstract method
  • @Deprecated: Marks element as deprecated
  • @SuppressWarnings: Suppresses compiler warnings
  • @SafeVarargs: Suppresses unsafe varargs warning
  • Custom Annotations: Defined using @interface
  • @Retention: Specifies when annotation is available
  • RetentionPolicy.SOURCE: Discarded by compiler
  • RetentionPolicy.CLASS: Available in class file (default)
  • RetentionPolicy.RUNTIME: Available at runtime
  • @Target: Specifies where annotation can be applied
  • value(): Special element name, can omit "value="

Post a Comment

0 Comments