Annotations introduced in Java 5.0, allow programmers to embed additional information called metadata into a Java source file. Annotations do not alter the execution of a program, but the information embedded using annotations can be used by various tools during development and deployment. They can be applied to package and type declarations (classes, interfaces, enums, and annotations), constructors, methods, fields, parameters, and variables.
As stated about annotations on the Oracle's official site
“Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.”
Annotations serve a lot of purposes in the java code, among them are:
There are two things we need to consider with annotations in java. One is the "annotation" itself; another is the "annotation type."We can use the annotations already provided by java (or other frameworks) or we may also define our custom annotations using “annotation type”.
Remember: We use “annotation type" when we want to create our own custom annotation.
Java defines seven built-in annotations. Among them some are used directly in java code(methods etc) while some are meant to annotate other annotations
Annotations that are applied directly to java code are:
Annotations applied to other annotations:
These annotations are imported from java.lang.annotation.@Retention
@Target (ElementType.FIELD)
@interface MyAnnotaion { }
Here in above example @MyAnnotation is the custom annotation to which the @Target annotation is applied.
In our code we can use this custom annotation @MyAnnotation only with the fields .
2.) Use Inherited to annotate a class, the annotation will be inherited by any subclass of the annotated class. If the
user queries the annotation type on a class declaration, and the class declaration has no annotation of this type,
then the class's parent class will automatically be queried for the annotation type. This process will be repeated until
an annotation of this type is found or the root class is reached.
Creating Custom Annotations:
We can create our own annotations and use it. Creating an annotation is similar to creating an interface. But the annotation declaration is preceded by an@ symbol.
Example of a custom annotation:
// specifying runtime retention policy
@Retention (RetentionPolicy.RUNTIME)
@interface MyAnnotation
{
String author(); // Annotation member
String date(); // Annotation member
}
After creating our custom annotation we can use it in our java code:
// applying annotation to the class
@MyAnnotation(author="me",date="7/1/2013")
public class Test {
// Applying annotation to the method
@MyAnnotation(author="me",date="7/1/2013")
public static void testMethod() {
System.out.println("JAVA");
System.out.println("Annotations");
}
public static void main(String args[ ]){
testMethod();
}
}
As stated about annotations on the Oracle's official site
“Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.”
Annotations serve a lot of purposes in the java code, among them are:
- Used by the compiler to detect errors or suppress warnings.
- Software tools can process annotation information to generate code, XML files, and so forth.
- Some annotations are available to be examined at runtime.
There are two things we need to consider with annotations in java. One is the "annotation" itself; another is the "annotation type."We can use the annotations already provided by java (or other frameworks) or we may also define our custom annotations using “annotation type”.
Remember: We use “annotation type" when we want to create our own custom annotation.
Java defines seven built-in annotations. Among them some are used directly in java code(methods etc) while some are meant to annotate other annotations
Annotations that are applied directly to java code are:
- @Override- Checks that the method is an override.When used with a java function it will cause a compile time warning if the method is not found in the parent class.
- @Deprecated - This annotation will mark the method as obsolete. It causes a compile warning if the method is used.
- @SuppressWarnings - This instructs the compiler to suppress the compile time warnings specified in the annotation parameters. This is something like telling the compiler that I know what I am doing so please do not warn me :)
Annotations applied to other annotations:
These annotations are imported from java.lang.annotation.@Retention
- @Retention - This meta annotation denotes the level till which this annotation will be carried- Whether in code only, compiled into the class, or available at runtime through reflection.
- SOURCE. Annotations are to be discarded by the Java compiler.
- CLASS. Annotations are to be recorded in the class file but not be retained by the JVM. This is the default value.
- RUNTIME. Annotations are to be retained by the JVM so you can query them using reflection.
- @Documented- When an annotation type is annotated with @Documented, then wherever this annotation is used those elements should be documented using Javadoc tool.
- @Target- This meta annotation tells that this annotation type is applicable for only the element (ElementType) listed. Possible values for ElementType are, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE.
@Target (ElementType.FIELD)
@interface MyAnnotaion { }
Here in above example @MyAnnotation is the custom annotation to which the @Target annotation is applied.
In our code we can use this custom annotation @MyAnnotation only with the fields .
- @Inherited-
2.) Use Inherited to annotate a class, the annotation will be inherited by any subclass of the annotated class. If the
user queries the annotation type on a class declaration, and the class declaration has no annotation of this type,
then the class's parent class will automatically be queried for the annotation type. This process will be repeated until
an annotation of this type is found or the root class is reached.
Creating Custom Annotations:
We can create our own annotations and use it. Creating an annotation is similar to creating an interface. But the annotation declaration is preceded by an@ symbol.
Example of a custom annotation:
// specifying runtime retention policy
@Retention (RetentionPolicy.RUNTIME)
@interface MyAnnotation
{
String author(); // Annotation member
String date(); // Annotation member
}
After creating our custom annotation we can use it in our java code:
// applying annotation to the class
@MyAnnotation(author="me",date="7/1/2013")
public class Test {
// Applying annotation to the method
@MyAnnotation(author="me",date="7/1/2013")
public static void testMethod() {
System.out.println("JAVA");
System.out.println("Annotations");
}
public static void main(String args[ ]){
testMethod();
}
}