Friday, November 13, 2015

annotations in java with example

Java Annotations Basics: 

  • Java annotations allows us to add metadata information to our source code
  • Annotations were added to the java from JDK 5. 
  • Java annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose.
  • An annotation always starts with the symbol @ followed by the annotation name. 
  • The symbol @ indicates to the compiler that this is an annotation.
For Example :
@Override
Here @ symbol represents that this is an annotation and the Override is the name of this annotation
                          

Use of annotations in java:


Instructions to the compiler:

                                        
There are three built-in annotations available in Java. They are

  • @Deprecated
  • @Override
  • @SuppressWarnings
More about these built-in annotations with example is discussed in the next sections of this article.

2) Compile-time instructors:

Annotations can provide compile-time instructions to the compiler that can be further used by sofware 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

Where we can use annotations: 

  • Annotations can be applied to the classes, interfaces, methods and fields. 
  • For example the below annotation is being applied to the method.
 @override
  void method(){
  //do something
 }

it is instructing compiler that myMethod() is a overriding method which is overriding the method (myMethod()) of super class.


Built-in annotations in java: 

                      Java has three built-in annotations:

  • @Override
  • @Deprecated
  • @SuppressWarnings

@Override:

  •                              While overriding a method in the child class, we should use this annotation                                to mark that method
  •                             This makes code readable and avoid maintenance issues

Example:


                         public class MyParentClass {

                              public void justaMethod() {
                              System.out.println("Parent class method");
    }
}


                       public class MyChildClass extends MyParentClass {

                     @Override
                      public void justaMethod() {
                     System.out.println("Child class method");
    }
}
      

2) @Deprecated

  • @Deprecated annotation indicates that the marked element (class, method or field) is deprecated and should no longer be used.
  •  The compiler generates a warning whenever a program uses a method, class, or field that has already been marked with the @Deprecated annotation.
  •  When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example
  • . Make a note of case difference with @Deprecated and @deprecated. @deprecated is used for documentation purpose.    

Example:

/**
* @deprecated
* reason for why it was deprecated
*/
 @Deprecated
 public void anyMethodHere(){
 / / Do something
}      

Now, whenever any program would use this method, the compiler would generate a warning

3) @SuppressWarnings:

  • This annotation instructs compiler to ignore specific warnings. 
  • For example in the below code, 
  • I am calling a deprecated method (lets assume that the method deprecatedMethod() is marked with @Deprecated annotation) 
  • so the compiler should generate a warning, however I am using @@SuppressWarnings annotation that would suppress that deprecation warning.     
@SuppressWarnings("deprecation")
void myMethod() {
myObject.deprecatedMethod();
}

Creating Custom Annotations:


  1. Annotations are created by using @interface, followed by annotation name as shown in the below example.
  2. All annotations extends java.lang.annotation.Annotation interface. Annotations cannot include any extends clause.
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
 public @interface MyCustomAnnotation{
 int studentAge() default 18;
String studentName();
String stuAddress();
String stuStream() default "CSE";
}

Note: All the elements that have default values set while creating annotations can be skipped while using annotation. For example if I’m applying the above annotation to a class then I would do it like this:



         

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...