Java Annotations Tutorial : Compiler Instructions Android

Android Annotations

Java Annotation is referred as metadata; it was introduced in JDK 5. The feature enables you to include supplemental information into source file. it does not change the program but it provides semantics of a program. Annotation is an effective way for creating source code; example source code generator identifies the annotation snippet and generates code according to that procedure. The term metadata is also used to refer annotation feature. Most Java development tools use annotation well. An annotation is created based on the interface. Let's look at simple Java annotation example.

Java Annotations Tutorial : Compiler Instructions Android

// simple annotation code

@interface SimpleAnnotation
float dvalue();
String databook();

     If you observe the above snippet, an interface keyword precedes @ symbol, it tells to compiler that an annotation type is being declared. The interface contains two methods dvalue(), and databook() you don't need to provide method definition, It acts like a pure interface.
These methods act much like fields; you can fetch it from below example.

Remember: We never use extends keyword with annotation because all annotation types automatically extend the annotation interface. Super interface is referred as Annotation. The Java annotation is come from java.lang.annotation package. No annotation can inherit another, they must return a primitive type (such as int, float) or class, string or enum or another annotation.

How to annotate a method

@SimpleAnnotation(dvalue=12.365, databook="Android is good")
public static void foo()

Most useful Android Annotations

Java provides many built-in annotations, android uses many of them. Let's look at the scope


You may familiar with @Override annotation in android development it can be used only on methods. It has a rule , a method annotated with @Override must override a method from super class. It makes sure that the super class method is overridden properly.


It's a type of marker annotation. It denotes old way of declaration or usage of methods. It recommends using new form, replacing the old forms.


It defines one or more warnings. It can be applied to any type of declaration.

Java Annotations Tutorial : Compiler Instructions Android Java Annotations Tutorial : Compiler Instructions Android Reviewed by Nikin on 12 January Rating: 5
Powered by Blogger.