Friday, January 03, 2014

Java Annotations

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")

If annotations have a single element named value, it can be omitted as shown below:
@SuppressWarnings("unchecked")

If annotations have no elements, then the braces can be omitted as well:
@Override





No comments: