An Explanation of Java Annotations

An Explanation of Java Annotations thumbnail
The @ symbol is used to denote an annotation in the Java language.

Annotations were added to the Java language in Java 1.5 and can easily add an entirely new dynamic to regular programming. Simply put, annotations are a way of adding metadata to different aspects of a class, extra information that can be used to change the way the class behaves or simply to further describe it in manners not easily enforceable via standard means.

  1. Using Annotations

    • Annotations are extremely simple to use, and can be applied to multiple areas of your code. An example of some annotation usage would be:

      @Deprecated

      public class MyClass ...

      Which would mark the class as "deprecated", meaning it shouldn't be used if at all possible. Another annotation useful for demonstrative purposes is the SuppressWarnings annotation:

      @SuppressWarnings("unused")

      public void somethingWithUnusedVariables() { ....

      With this example, you can see that you can pass information to the annotation -- in this case, the kind of warnings to suppress -- which can then be used by different tools, such as the compiler.

    What Is an Annotation?

    • In Java code, an annotation is little more than a regular interface. Creating one is extremely simple:

      public @interface MyAnnotation {

      }

      This new annotation can be used anywhere, such as to decorate a class:

      @MyAnnotation

      public class SomeClass...

      Of course, in this form, it's rather useless -- it contains no data and by default all annotations are stored with the class file but are not accessible at runtime, so they serve as little more than an extra comment.

    Creating Useful Annotations

    • As stated previously, by default annotations are unavailable at runtime. This is fine if you're simply using it as a marker of some kind for other developers to take note of, or if you're going to do some bytecode-level processing, but in order to access your annotation at runtime (via reflection) you need to tell the compiler to do so. You can do this with anotherent annotation that you apply to your new annotation:

      @Retention(RetentionPolicy.RUNTIME)

      This will allow you to access the annotation information -- through reflection -- while your code is running and process it accordingly.

    Reading Annotation Information

    • Reading annotation information at runtime is a little tricky, but by no means impossible. Using Java's reflection functionality, you can get the annotations that are applied to a class, method or field, as well as any information included in that annotation. For example, consider the following simple annotation:

      @Retention(RetentionPolicy.RUNTIME)

      public @interface Author {

      String value();

      }

      Applied to a class like so:

      @Author("John Doe")

      public class MyClass {...

      You can get the author information with the following code:

      final Author author = MyClass.class.getAnnotation(Author.class);

      System.out.println(author.value());

Related Searches:

Resources

  • Photo Credit Comstock/Comstock/Getty Images

Comments

Related Ads

Featured