Practice Programs on Custom Annotations

1. Create and Use a Simple Custom Annotation

This program demonstrates how to create a simple custom annotation. The annotation is applied to a method and accessed using reflection.

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

@interface MyAnnotation {

    String message();
}

class Demo {

    @MyAnnotation(message = "Welcome to Java Annotations")
    public void display() {

        System.out.println("Display Method Executed");
    }
}

public class SimpleAnnotationExample {

    public static void main(String[] args) throws Exception {

        Demo obj = new Demo();

        obj.display();

        Method method = Demo.class.getMethod("display");

        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);

        System.out.println("Annotation Message: " + annotation.message());
    }
}
Output:
Display Method Executed
Annotation Message: Welcome to Java Annotations

2. Student Information Using Custom Annotation

This program demonstrates a custom annotation with multiple elements. The annotation stores student information and retrieves it using reflection.

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

@interface StudentInfo {

    int id();

    String name();
}

class Student {

    @StudentInfo(id = 101, name = "Rahul")
    public void details() {

        System.out.println("Student Details");
    }
}

public class StudentAnnotationExample {

    public static void main(String[] args) throws Exception {

        Student obj = new Student();

        obj.details();

        Method method = Student.class.getMethod("details");

        StudentInfo info = method.getAnnotation(StudentInfo.class);

        System.out.println("Student ID : " + info.id());
        System.out.println("Student Name : " + info.name());
    }
}
Output:
Student Details
Student ID : 101
Student Name : Rahul

3. Author Information Using Custom Annotation

This program creates a custom annotation to store author details. The annotation values are accessed at runtime using reflection.

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

@interface Author {

    String name();

    String version();
}

class Book {

    @Author(name = "Raja", version = "1.0")
    public void showBook() {

        System.out.println("Java Programming Book");
    }
}

public class AuthorAnnotationExample {

    public static void main(String[] args) throws Exception {

        Book obj = new Book();

        obj.showBook();

        Method method = Book.class.getMethod("showBook");

        Author author = method.getAnnotation(Author.class);

        System.out.println("Author : " + author.name());
        System.out.println("Version : " + author.version());
    }
}
Output:
Java Programming Book
Author : Raja
Version : 1.0Code language: CSS (css)

4. Employee Details Using Custom Annotation

This program demonstrates the use of a custom annotation for employee details. The annotation values are displayed using Java Reflection API.

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

@interface EmployeeInfo {

    int id();

    String department();
}

class Employee {

    @EmployeeInfo(id = 201, department = "Software")
    public void employeeDetails() {

        System.out.println("Employee Information");
    }
}

public class EmployeeAnnotationExample {

    public static void main(String[] args) throws Exception {

        Employee obj = new Employee();

        obj.employeeDetails();

        Method method = Employee.class.getMethod("employeeDetails");

        EmployeeInfo info = method.getAnnotation(EmployeeInfo.class);

        System.out.println("Employee ID : " + info.id());
        System.out.println("Department : " + info.department());
    }
}
Output:
Employee Information
Employee ID : 201
Department : Software

5. Course Details Using Custom Annotation

This program creates a custom annotation to store course details. The annotation information is retrieved and displayed at runtime.

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)

@interface Course {

    String courseName();

    int duration();
}

class Training {

    @Course(courseName = "Java Programming", duration = 45)
    public void courseDetails() {

        System.out.println("Course Information");
    }
}

public class CourseAnnotationExample {

    public static void main(String[] args) throws Exception {

        Training obj = new Training();

        obj.courseDetails();

        Method method = Training.class.getMethod("courseDetails");

        Course course = method.getAnnotation(Course.class);

        System.out.println("Course Name : " + course.courseName());
        System.out.println("Duration : " + course.duration() + " Days");
    }
}
Output:
Course Information
Course Name : Java Programming
Duration : 45 Days

References

CodeGPT-small-java
Codebert-base
CodeBERT
Scroll to Top