Top 10 most frequently used Annotation example

Here we are sharing examples of  most frequently used annotations . Annotation example helps in understanding the implementation and syntax of the annotation , that is , how and in which scenario .  Annotation example which we are discussing here are as follows :

Read Also :   Java Annotation tutorials for beginners  
         
                        How to create custom annotation in java
                     

1.   @Deprecated  Annotation  example

@Deprecated: This marks the function as obsolete (to be old/will be removed).
Definition of Deprecated: Said of a construct in a computing language considered old and planned to be removed out, but still available for use. Deprecated is from java.lang hence no import required


package com.sample.annotation;
public class ClassThatContainsTheDeprecatedMethod {
    @Deprecated
    public void deprecatedMethod() 
    {
      System.out.println(" This method is deprecated "); 
    }
}


 2.   @SuppressWarnings  Annotation example

@SuppressWarnings("deprecation") : The value deprecation indicates that all deprecation related warnings shall be suppressed.

package com.sample.annotation;
 
@SuppressWarnings("deprecation")
public class Sample {
    public static void main(String args[]) {
        ClassThatContainsTheDeprecatedMethod obj = 
                                   new ClassThatContainsTheDeprecatedMethod ();
        obj.deprecatedMethod ();
    }
}


 3.    @Override Annotation  example


Override, symbolizes that class Sub overrides the method display of class Super .




Create a class called Super as below


package com.sample.annotation;

public class Super {
    public void display() {
        System.out.println("I am from Super");
    }
}

Create a class called Sub as below,


package com.sample.annotation;

public class Sub extends Super {
    @Override
    public void display() {
        System.out.println("I am from Sub");
    }
}


Create another class called Sample as below,

package com.sample.annotation;
public class Sample {
    
    public static void main(String args[]) {
        Super obj = new Sub();
        obj.display();
    }
    
}

Try compiling and running Sample, it should result in


I am from Sub


java annotation exmaple






 4.    @Documented  Annotation  example

@Documented This marks the details of an annotation to be included in the documentation of the class where it is getting used.





package com.sample.annotation;
import java.lang.annotation.Documented;
@Documented
public @interface MySampleAnnotation {
    public String name();
    public int age();
}


5.   @Target Annotation  example

This marks another annotation to restrict what kind of java elements the annotation may be applied to


package com.sample.annotation;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target (value = ElementType.TYPE)
public @interface MySampleAnnotation {
    public String name();
    public int age();
}

ElementType indicates what type of elements (java source elements); this annotation can be applied to. Here TYPE indicates classes and other types


6.   @Retention Annotation example

@Retention(RetentionPolicy.RUNTIME)This means that it is available during actual execution of the program (through reflection)

 package com.sample.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MySampleAnnotation {
    public String name();
    public int age();
}


JUNIT ANNOTATION EXAMPLE


Let us discuss in detail about the above marked annotations

7. @BeforeClass  : is the first to be called by the JUNIT framework . The resources that @BeforeClass initializes are required by all the testing components in global.

8. @Before and @After :are the components that run before and after each actual unit tester.


package com.ofss.test;
import junit.framework.Assert;
import org.junit.*;
public class MySampleTest {
    @BeforeClass
    public static void startClass() throws Exception {
        System.out.println("startClass");
    }
    @Before
    public void startMethod() throws Exception {
        System.out.println("startMethod ");
    }
    @Test
    public void myTestMethod() throws Exception {
        System.out.println("myTestMethod ");
        Assert.assertNull(null);
    }
    @After
    public void endMethod() throws Exception {
        System.out.println("endMethod ");
    }
    @AfterClass
    public static void endClass() throws Exception {
        System.out.println("endClass ");
    }
}




9. @Test : one that forms the core of test case.

10. @AfterClass :  is the last to be called by the JUNIT framework .The resources that Test Engine Starter sets up will be cleaned up by the @AfterClass  .

Please mention in the comments in case if you are looking for another annotation example which is not mentioned in this post .

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry