What are annotations in Java? How are they used?
What are annotations in Java? How are they used?
329
18-Jul-2024
Ravi Vishwakarma
18-Jul-2024Annotations 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: