What are Annotations?
Annotations are basically syntactic meta-data that can be applied to packages, classes, methods, variables & parameters.
Purpose
Annotations serve several purposes such as...
- Provide extra information to the compiler
- @Override
- @SuppressWarnings
- @Deprecated
- Reduce boiler plate code
- See project LomBok
- AOP as in Spring AOP
- Replacement for marker interfaces
- Used by frameworks to glue user defined classes together into an application instead of specifying the same in an external XML configuration file
- Spring
- Provide information to be used at run-time
Examples
Annotations can have elements which have values
@Author(name="kirk", date="1/1/2014")
Annotations can have elements which have values
@Author(name="kirk", date="1/1/2014")
If annotations have a single element named value, it can be omitted as shown below:
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
If annotations have no elements, then the braces can be omitted as well:
@Override
@Override
References
Java documentation: http://docs.oracle.com/javase/tutorial/java/annotations/
Wikipedia: http://en.wikipedia.org/wiki/Java_annotation
Java documentation: http://docs.oracle.com/javase/tutorial/java/annotations/
Wikipedia: http://en.wikipedia.org/wiki/Java_annotation
No comments:
Post a Comment