Java Annotations are a powerful feature that provide metadata about a program. They are widely used in modern Java frameworks such as Spring, Hibernate, and testing frameworks. Annotations help developers add additional information to classes, methods, fields, and parameters without changing the actual business logic.
Understanding built-in and custom annotations is important for developers who want to work with advanced Java applications and frameworks.
What are Java Annotations?
Annotations are metadata added to Java code that provide information to the compiler or runtime environment.
Annotations do not directly affect program execution but can influence how programs are processed by tools and frameworks.
Example:
@Override
public String toString() {
return "Student";
}
Here, @Override is an annotation that tells the compiler that the method overrides a method from the parent class.
Why Are Annotations Used?
Annotations provide several advantages:
Improve code readability
Reduce configuration using XML files
Provide additional information to frameworks
Enable compile-time and runtime processing
Simplify framework development
Built-in Java Annotations
Java provides several built-in annotations commonly used in development.
@Override
This annotation indicates that a method overrides a method in the superclass.
Example:
@Override
public void display() {
System.out.println("Display Method");
}
It helps the compiler check whether the method correctly overrides a parent class method.
@Deprecated
The @Deprecated annotation indicates that a method or class is no longer recommended for use.
Example:
@Deprecated
public void oldMethod() {
}
When developers use this method, the compiler shows a warning.
@SuppressWarnings
This annotation is used to suppress compiler warnings.
Example:
@SuppressWarnings("unchecked")
List list = new ArrayList();
This prevents unnecessary compiler warnings during compilation.
@FunctionalInterface
This annotation is used in Java 8 to indicate that an interface contains only one abstract method.
Example:
@FunctionalInterface
interface MyInterface {
void show();
}
It is commonly used with Lambda Expressions.
What Are Custom Annotations?
Java also allows developers to create their own annotations. These are called Custom Annotations.
Custom annotations are useful when building:
Frameworks
Validation tools
Logging systems
Configuration-based applications
Creating a Custom Annotation
Custom annotations are created using the @interface keyword.
Example:
@interface Author {
String name();
int version();
}
This creates a custom annotation called Author.
Using Custom Annotation
After creating an annotation, it can be used like this:
@Author(name="Harish", version=1)
class MyClass {
}
This attaches metadata to the class.
Retention Policies
Annotations can be retained at different stages using RetentionPolicy.
SOURCE – Available only in source code
CLASS – Stored in the class file
RUNTIME – Available during runtime using reflection
Example:
@Retention(RetentionPolicy.RUNTIME)
@interface Author {
String name();
}
Target Annotations
The @Target annotation defines where an annotation can be applied.
Possible targets include:
TYPE (class, interface)
METHOD
FIELD
CONSTRUCTOR
PARAMETER
Example:
@Target(ElementType.METHOD)
@interface TestAnnotation {
}
Real-World Usage of Annotations
Annotations are heavily used in modern Java frameworks.
Examples include:
Spring Framework –
@Component,@Autowired,@ServiceSpring Boot –
@SpringBootApplicationHibernate –
@Entity,@TableJUnit –
@Test
These annotations help reduce configuration and simplify development.
Conclusion
Java Annotations provide a powerful way to add metadata to Java programs. Built-in annotations help improve code quality and readability, while custom annotations allow developers to create flexible and dynamic applications.
Understanding annotations is essential for developers working with modern frameworks such as Spring and Hibernate.
If you want to learn Java from the basics to advanced concepts with practical examples, joining Core JAVA Online Training can help you build strong programming skills and prepare for real-world development.

No comments:
Post a Comment