Annotation Types in Java
Types of Annotation in Java
In order to add or add-on program information we use Annotations Types in Java. The language uses annotations to provide more information about the program.
These annotations are the tags that are attached to classes, interfaces, methods, and fields to designate extra information that the Java compiler and Java Virtual Machine may use. It is an alternative to XML (Extensible Markup Language ) and Java marker interfaces since Annotations types in Java are used to offer more information.
Annotations in Java
Annotations can be used to add extra details about a program.
- It begins with – “@.”
- They do not affect how a built program behaves.
- Instance variables, constructors, methods, classes, and other program elements can all be annotated to add metadata (information).
- As they can alter how a program is handled by the compiler, annotations are not simply comments. See the code example below.
- An alternative to XML and Java marker interfaces, annotations are essentially used to provide extra information.
Predefined Types of Java Annotations
There are seven built-in annotations in Java.
- The four imported from java.lang.annotation
@Retention, @Documented, @Target, and @Inherited
To what level an annotation will be visible is specified using the @Retention annotation.
Annotation for @Retention It establishes the location and duration of the annotation’s retention. The @Retention annotation has three possible values:
- SOURCE: The compiler will not take into account annotations; they will remain at the source level.
- CLASS: The JVM ignores annotations, but they are kept at build time.
- RUNTIME: At runtime, these will be kept.
Its syntax is:
@Retention(RetentionPolicy)
A tool is informed by a marker interface that an annotation has to be documented. Javadoc comments do not contain annotations. By using the @Documented annotation in the code, tools like Javadoc may process it and add information about the annotation type in the document that is produced.
For example,
@Documented
public @interface MyCustomAnnotation{ ... }
- It is meant to be used just as a justification for another annotation. One argument, which must match the ElementType count, is required for @Target.
- The type of assertions that Java annotations can connect to are determined by this annotation. The constants are listed below with the category of affirmation that each one relates to.
Target constant | Annotations can be applied to |
---|---|
ElementType.ANNOTATION TYPE | An annotation type may be assigned to using |
ElementType.CONSTRUCTOR. | Using a function Object() { [native code] } may be treated. |
ElementType.FIELD. | A field or property can be used |
ElementType.LOCAL VARIABLE | A local variable may be treated |
ElementType.METHOD. | A method-level annotation |
ElementType.PACKAGE | A package declaration can use this keyword |
ElementType.PARAMETER. | The parameters of a method can be changed |
ElementType.TYPE. | Any element of a class can be affected |
The marker annotation @Inherited can only be used on the annotation declaration. Only annotations used in class definitions are impacted. The superclass’s annotation is passed down to the subclass via the @Inherited annotation.
As a result, if a subclass is asked for a specific annotation and that annotation is missing from the subclass, the superclass is checked. It yields that annotation if both it and the @Inherited annotation are present in the superclass.
Its syntax is:
@Inherited
- The three in java.lang
@Deprecated, @Override, and @SuppressWarnings
- A marker annotation, that is. It signifies that a statement has been superseded by a newer form and is no longer valid.
- When an element has been deprecated, the Javadoc @deprecated tag should be used.
- Both the @deprecated tag and the @Deprecated annotation are used for runtime reflection.
- When both are used, the @deprecated tag gets priority over the @Deprecated annotation.
The syntax is
@Deprecated
accessModifier returnType deprecatedMethodName() { ... }
This marker annotation is restricted to methods only. A method from a superclass must be overridden by a method annotated with @Override. A compile-time error will occur if it doesn’t.
error: method does not override or implement a method from a supertype @Override
It is used to make sure that a method from a superclass is actually overridden rather than just overloaded.
- The compiler is instructed to suppress certain compiler warnings using this information.
- The names of the warnings to suppress are given in string form. Any declaration type may use this kind of annotation.
- Java divides warnings into two groups. They are unchecked and deprecated.
- When a legacy code interacts with a generics-using code, any unchecked warning is generated.
To suppress a particular category of warning, we use:
@SuppressWarnings("warningCategory")
For example,
@SuppressWarnings("deprecated")
To suppress multiple categories of warnings, we use:
@SuppressWarnings({"warningCategory1", "warningCategory2"})
For example,
@SuppressWarnings({"deprecated", "unchecked"})
Example Code for @Deprecated method
import java.util.*; public class Main{ // We're in the deprecated method which is replaced by the newMethod() @Deprecated public static void PrepInsta(){ System.out.println("I'm in Deprecated method of PrepInsta"); } public static void main(String args[]) { PrepInsta(); } }
Output: I'm in Deprecated method of PrepInsta
Categories of Annotations Types in Java
There are generally 5 types of annotations.
What use do Java Annotations serve?
Prime Course Trailer
Related Banners
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment