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
2. @SuppressWarnings Annotation example
@SuppressWarnings("deprecation") : The value deprecation indicates that all deprecation related warnings shall be suppressed.
3. @Override Annotation example
Override, symbolizes that class Sub overrides the method display of class Super .
Create a class called Super as below
Create a class called Sub as below,
Create another class called Sample as below,
Try compiling and running Sample, it should result in
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.
5. @Target Annotation example
This marks another annotation to restrict what kind of java elements the annotation may be applied to
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)
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.
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 .