Annotation :
Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or
fields to indicate some additional information which can be used by java compiler and JVM.
Annotations in java are used to provide additional information, so it is an alternative option for XML and
java marker interfaces
What’s the use of Annotations?
1) Instructions to the compiler: There are three built-in annotations available in Java (@Deprecated,
@Override & @SuppressWarnings) that can be used for giving certain instructions to the compiler. For
example the @override annotation is used for instructing compiler that the annotated method is
overriding the method.
2) Compile-time instructors: Annotations can provide compile-time instructions to the compiler that can
be further used by software build tools for generating code, XML files etc.
3) Runtime instructions: We can define annotations to be available at runtime which we can access
using java reflection and can be used to give instructions to the program at runtime.
Annotation basics
An annotation is created through a mechanism based on the interface.
A Java annotation in its shortest form looks like this:
Syntax: @interface MyAnno
The @ that precedes the keyword interface. This tells the compiler that an annotation type is being
declared The name following the @interface character is the name of the annotation.
All annotations consist solely of method declarations.
However, you don’t provide bodies for these methods. Instead, Java implements these methods.
Moreover, the methods act much like fields.
Annotations can be applied to the classes, interfaces, methods and fields. For example the below
annotation is being applied to the method.
@Override void myMethod()
{ //Do something
}
An annotation cannot include an extends clause. However, all annotation types automatically
extend the Annotation interface. Thus, Annotation is a super-interface of all annotations. It is
declared within the java.lang.annotation package.
It overrides hashCode( ), equals( ), and toString( ), which are defined by Object. It also specifies
annotationType( ), which returns a Class object that represents the invoking annotation. Program:
To display welcome message.
class annu
{
@mymethod public void show()
{
System.out.println("Well come to Annotation");
}
public static void main(String a[])
{
annu h = new annu();
h.show(); //deprecated
}
}
OUTPUT: Well come to Annotation
The Built-In Annotations:
Java defines seven built-in annotations out of which three (@Override, @Deprecated, and
@SuppressWarnings) are applied to Java code and they are included in java.lang library. These three
annotations are called regular Java annotations.
Rest four (@Retention, @Documented, @Target, and @Inherited) are applied to other annotations and
they are included in java.lang.annotation library. These annotations are called meta Java annotations.
@Override: @Override is a marker annotation that can be used only on methods. A method annotated
with @Override must override a method from a superclass. If it doesn’t, a compile-time error will result.
It is used to ensure that a superclass method is actually overridden, and not simply overloaded
Example:
public class Animal
{
public void makeSound()
{
}
}
class Cat extends Animal
{
@Override
public void makeSound()
{
System.out.println("myyyyyaaawwwwww");
}
}
@Deprecated
Use this annotation on methods or classes which you need to mark as deprecated. Any class that will try
to use this deprecated class or method, will get a compiler “warning“.
@Deprecated public Integer myMethod() { return null; }
@SuppressWarnings
This annotation instructs the compiler to suppress the compile time warnings specified in the annotation
parameters. e.g. to ignore the warnings of unused class attributes and methods use
@SuppressWarnings("unused") either for a given attribute or at class level for all the unused attributes
and unused methods.
@SuppressWarnings("unused")
public class DemoClass
{ //@SuppressWarnings("unused")
private String str = null;
//@SuppressWarnings("unused")
private String getString()
{
return this.str;
}
}