A Brief Introduction to Annotations in Java

Posted By : Neha Yadav | 30-Oct-2018

 

 

Annotaions:-
Annoations come in JDK 1.5. And are widely used in
java application development.
Annotaion provide information about the class, methods, fields etc. They do not effect the business logic of the application.
They do not effect
the code at run time.

There are built-in annoations as well as we can create our custom annoations.
Some built in
annotations are 
1. @Override
2. @Deprecated
3. @SupressWarnings

1. @Override:- This annotation is used for the indication that a child class is overriding the method of the parent class. This improve the readability of code and resolve the issue of the maintainance like if parent class is changing the prototype of the method then child class also.

 

class A{
void show(){
System.out.println("Class A");
}
}
class B extends A{
@Override
void show(){
System.out.println("Class B");}
}

2. @Deprecated:- This annotation is used for the indication that a certain method, class etc will no longer be used in the java higher versions.

 

class A{
@Deprecated
void show(){
System.out.println("Class A");
}
}

3. @SupressWarnings:- This annotation is used to supress the given warnings on the classes, methods etc.

 

class A{
@Deprecated
static void show(){
System.out.println("Class A");
}
}

class B{
@SupressWarnings("deprecation")
void show1(){
A.show();}
}


We can also create custom annotations according to our requirement. For this we use certain annotations of java.

1. @Interface:- This annotation is used to create custom annotation.
Example:-

 

 

public @Interface HelloAnno{
String name() default "oodles";
}

usage:
@HelloAnno
public class ABC{
//...body
}

2. @Documented: This annoation indicated that certain classes which this annoation would be documented by java doc.
Example:-

 

 

@Documented
public @Interface HelloAnno{
String name() default "oodles";
}

usage:
@HelloAnno
public class ABC{
//...body
}

3. @Target :- This annoation indicates that where we can use the annoation like on class, on methods etc.

Example:-

 

 

@Target({ElementType.METHOD})
@Documented
public @Interface HelloAnno{
String name() default "oodles";
}

usage:

public class ABC{
//...body

@HelloAnno
void show(){
System.out.println("class ABC");
}

}

An annotation can have following possible Target values:-
ElementType.METHOD
ElementType.PACKAGE
ElementType.PARAMETER
ElementType.TYPE
ElementType.ANNOTATION_TYPE
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.FIELD

4. @Inherited:- This annotation indicated that custom annotation used on the parent will be applicable on the child classes. 
Example:-

 

@Inherited
public @interface @HelloAnno {

}
@HelloAnno
public class ABC { 
  //body... 
}
public class XYZ extends ABC { 
  //body ... 
}

 

5. @Retention:- This annoatation indicated that how long annoation will be applicable on class, method etc or when.
Example

 

@Retention(RetentionPolicy.RUNTIME)
@interface HelloAnno {
   //body.. 
}

We can use different-different retention policies:-
RetentionPolicy.RUNTIME: Annotation applicable for runtime only.
RetentionPolicy.CLASS: Annotation applicable for .class only.
RetentionPolicy.SOURCE
:Annotation applicable for source code only.

 

About Author

Author Image
Neha Yadav

Neha is a creative person. She is having good knowledge of core java, advance java, hibernate,spring boot. She likes to solve puzzles, sudoku. She is a fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..