Java Annotations:
Description:
Java Annotation is a tag that represents the metadata to indicate some additional information which can be used by java compiler and JVM. i.e. attached with class, interface, methods or fields
Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces.
Types of Annotations:
There are two types,
1)Built-In Java Annotations:
There are several built-in annotations in java. Some annotations are below,
Built-In Java Annotations used in java code
Let's start with the built-in annotations first
@Override
@Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs.
Note: Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden.
else that method which we define acts like new method not the override method.
Using Annotation: gives assurity of method overriding etc.,
Without Annotation: Method acts as new .
@SuppressWarnings
@SuppressWarnings annotation: is used to suppress warnings issued by the compiler.
Sample code:
package SampleExcercises;
import java.util.*;
class Annotation2 {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add("Mano");
list.add("Chella");
list.add("Prasath");
for (Object obj : list) {
System.out.println(obj);
}
}
}
Note: Since, we are using non-generic collection. If you remove the @SuppressWarnings("unchecked") annotation, it will show warning at compile time.
@Deprecated
@Deprecated annoation marks that this method is deprecated, so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods.
Sample code:
package SampleExcercises;
class A{
void m(){System.out.println("hello m");}
@Deprecated
void n(){System.out.println("hello n");}
}
class Annotation3{
public static void main(String args[]){
A a=new A();
a.n();
}}
2)Custom Annotations:
Java Custom annotations or Java User-defined annotations are easy to create and use.
NOTE: The @interface element is used to declare an annotation.
Example:
@interface MyAnnotation{}
MyAnnotation is the custom annotation name.
Points to remember for java custom annotation signature:
Types of Custom Annotations:
There are three types of Custom Annotations
Description:
Java Annotation is a tag that represents the metadata to indicate some additional information which can be used by java compiler and JVM. i.e. attached with class, interface, methods or fields
Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces.
Types of Annotations:
There are two types,
- Built-in Java annotations
- Custom annotations
1)Built-In Java Annotations:
There are several built-in annotations in java. Some annotations are below,
Built-In Java Annotations used in java code
- @Override
- @SuppressWarnings
- @Deprecated
Let's start with the built-in annotations first
@Override
@Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs.
Note: Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden.
else that method which we define acts like new method not the override method.
Using Annotation: gives assurity of method overriding etc.,
Without Annotation: Method acts as new .
@SuppressWarnings
@SuppressWarnings annotation: is used to suppress warnings issued by the compiler.
Sample code:
package SampleExcercises;
import java.util.*;
class Annotation2 {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add("Mano");
list.add("Chella");
list.add("Prasath");
for (Object obj : list) {
System.out.println(obj);
}
}
}
Note: Since, we are using non-generic collection. If you remove the @SuppressWarnings("unchecked") annotation, it will show warning at compile time.
@Deprecated
@Deprecated annoation marks that this method is deprecated, so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods.
Sample code:
package SampleExcercises;
class A{
void m(){System.out.println("hello m");}
@Deprecated
void n(){System.out.println("hello n");}
}
class Annotation3{
public static void main(String args[]){
A a=new A();
a.n();
}}
2)Custom Annotations:
Java Custom annotations or Java User-defined annotations are easy to create and use.
NOTE: The @interface element is used to declare an annotation.
Example:
@interface MyAnnotation{}
MyAnnotation is the custom annotation name.
Points to remember for java custom annotation signature:
- Method should not have any throws clauses
- Method should return one of the following: primitive data types, String, Class, enum or array of these data types.
- Method should not have any parameter.
- We should attach @ just before interface keyword to define annotation.
- It may assign a default value to the method.
Types of Custom Annotations:
There are three types of Custom Annotations
- Marker Annotation
- Single-Value Annotation
- Multi-Value Annotation
1)Marker Annotation
An annotation that has no method, is called marker annotation.
Example:
@interface MyAnnotation{}
The @Override and @Deprecated are marker annotations.
2) Single-Value Annotation
An annotation that has one method, is called single-value annotation.
Example:
@interface MyAnnotation{
int value();
}
We can provide the default value to method as well.
Example:
@interface MyAnnotation{
int value() default 0;
}
How to apply Single-Value Annotation:
Let's see the code to apply the single value annotation.
@MyAnnotation(value=10)
Note: The value can be anything.
3) Multi-Value Annotation:
An annotation that has more than one method, is called Multi-Value annotation.
Example:
@interface MyAnnotation{
int value1();
String value2();
String value3();
}
}
We can provide the default value to method as well.
Example:
@interface MyAnnotation{
int value1() default 1;
String value2() default "";
String value3() default "abs";
}
How to apply Multi-Value Annotation:
Let's see the code to apply the multi-value annotation.
@MyAnnotation(value1=1,value2="Mano",value3="Chennai")
Built-in Annotations used in custom annotations
- @Target
- @Retention
- @Inherited
- @Documented
@Target
@Target tag is used to specify at which type, the annotation is used.
The java.lang.annotation.ElementType enum declares many constants to specify the type of element where annotation is to be applied such as TYPE, METHOD, FIELD etc.,
Element Types | Where the annotation can be applied |
---|---|
TYPE | class, interface or enumeration |
FIELD | fields |
METHOD | methods |
CONSTRUCTOR | constructors |
LOCAL_VARIABLE | local variables |
ANNOTATION_TYPE | annotation type |
PARAMETER | parameter |
Example to specify annoation for a class
@Target(ElementType.TYPE)
@interface MyAnnotation
{
int value1();
String value2();
}
Example to specify annotation for a class, methods or fields
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@interface MyAnnotation
{
int value1();
String value2();
}
@Retention
@Retention annotation is used to specify to what level annotation will be available.
RetentionPolicy | Availability |
---|---|
RetentionPolicy.SOURCE | refers to the source code, discarded during compilation. It will not be available in the compiled class. |
RetentionPolicy.CLASS | refers to the .class file, available to java compiler but not to JVM . It is included in the class file. |
RetentionPolicy.RUNTIME | refers to the runtime, available to java compiler and JVM . |
Example to specify the RetentionPolicy
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation{
int value1();
String value2();
}
@Inherited
By default, annotations are not inherited to subclasses. The @Inherited annotation marks the
annotation to be inherited to subclasses.
Example:
@Inherited
@interface MyAnnotation { }//Now it will be available to subclass also
@Documented
The @Documented Marks the annotation for inclusion in the documentation.
It was very nice article and it is very useful to Testing tools learners.We also provide Big Data Hadoop Online Course Hyderabad
ReplyDelete