There are two types of annotations available with JDK5:
- Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.
- Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.
Simple Annotation
- Override
- Deprecated
- Suppresswarnings
Override
1 2 3 4 5 6 7 8 9 |
package www.technicaljungle.com; public class SimpleAnnotationOverride { @Override public String toString() { return super.toString() + "TechincalJungle.com Tutorial "; } } |
1 2 3 4 5 6 7 8 9 |
package www.technicaljungle.com; public class SimpleAnnotationOverride { @Override public String tostring() { return super.toString() + "TechincalJungle.com Tutorial "; } } |
1 2 |
Compile Time Issue: The method tostring() of type SimpleAnnotationOverride must override or implement a supertype method The method tostring() of type SimpleAnnotationOverride must override or implement a supertype method |
Deprecated
1 2 3 4 5 6 7 8 9 |
package www.technicaljungle.com; public class SampleDeprecated { @Deprecated public void doSomething() { // this method will be strikethrough. System.out.println("Testing annotation name: 'Deprecated'"); } } |
1 2 3 4 5 6 7 8 9 10 |
package www.technicaljungle.com; public class SimpleAnnotationDeprecated { public static void main(String [] args) { SampleDeprecated dep = new SampleDeprecated(); //this method will be strikethrough. dep.doSomething()<span style="text-decoration: line-through;">;</span> } } |
1 |
Output : Testing annotation name: 'Deprecated' |
After warning also output will be produced.
1 |
Compile Time Warning: The method doSomething() from the type SampleDeprecated is deprecated |
Suppresswarnings
For fixing above “deprecated” warning compiler will provide quick fix for it too. So if Supresswarning is been applied then compiler will not show any warning, if at all “deprecate’ warning will be flagged. But big YES, other warnings will definitely will be raised by compiler.
1 2 3 4 5 6 7 8 9 10 |
package www.technicaljungle.com; public class SimpleAnnotationDeprecated { @SuppressWarnings("deprecation") public static void main(String [] args) { SampleDeprecated dep = new SampleDeprecated(); dep.doSomething(); } } |
@Annotations