Annotations in Java are a form of metadata that can be added to Java code (classes, methods, variables, etc.). They provide information to the compiler and can be used at runtime by the JVM or other tools for various purposes.
Types of Annotations
Standard Annotations: Java provides several built-in annotations in the
java.lang package.
@Override: Indicates that a method is overriding a method in a superclass.
@Deprecated: Marks a method or class as deprecated, meaning it should not be used.
@SuppressWarnings: Tells the compiler to suppress specific warnings.
public class MyClass {
@Override
public String toString() {
return "MyClass";
}
@Deprecated
public void oldMethod() {
// Do something
}
@SuppressWarnings("unchecked")
public void myMethod() {
// Do something
}
}
Meta-Annotations: Meta-annotations are annotations that apply to other annotations. Some of the meta-annotations are:
@Retention: Specifies how long the annotation is retained (runtime, class file, source).
@Target: Specifies the kinds of elements an annotation type applies to (methods, fields, classes).
@Documented: Indicates that an annotation should be documented by Javadoc and similar tools.
@Inherited: Indicates that an annotation type is automatically inherited.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Annotations in Java are a form of metadata that can be added to Java code (classes, methods, variables, etc.). They provide information to the compiler and can be used at runtime by the JVM or other tools for various purposes.
Types of Annotations
Standard Annotations: Java provides several built-in annotations in the
java.langpackage.@Override: Indicates that a method is overriding a method in a superclass.@Deprecated: Marks a method or class as deprecated, meaning it should not be used.@SuppressWarnings: Tells the compiler to suppress specific warnings.Meta-Annotations: Meta-annotations are annotations that apply to other annotations. Some of the meta-annotations are:
@Retention: Specifies how long the annotation is retained (runtime, class file, source).@Target: Specifies the kinds of elements an annotation type applies to (methods, fields, classes).@Documented: Indicates that an annotation should be documented by Javadoc and similar tools.@Inherited: Indicates that an annotation type is automatically inherited.Example: