Understanding Java Annotation – Annotation Examples

java annotation Understanding Java Annotation   Annotation Examples

An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time. It is possible to create meta-annotations out of the existing ones in Java, which makes this concept more sophisticated.

Java defines a set of annotations that are built into the language.

Annotations applied to java code:

  • @Override - Checks that the function is an override. Causes a compile warning if the function is not found in one of the parent classes.
  • @Deprecated - Marks the function as obsolete. Causes a compile warning if the function is used.
  • @SuppressWarnings - Instructs the compiler to suppress the compile time warnings specified in the annotation parameters

Annotations applied to other annotations:

  • @Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at runtime through reflection.
  • @Documented - Marks another annotation for inclusion in the documentation.
  • @Target - Marks another annotation to restrict what kind of java elements the annotation may be applied to.
  • @Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).

Let’s take a look at these via sample Examples:

1) @Override Annotation:

Its presence indicates to the compiler that the annotated method must override an existing superclass method.

The most common use case for @Override is with Object methods:

The main reason @Override was created was to deal with simple typographical errors. For example, a method mistakenly declared as

is in fact not an override – the method name has all lower case letters, so it doesn’t exactly match the name of the hashValue()method. It will however compile perfectly well. Such an error is easy to make, and difficult to catch, which is a dangerous combination. Using the @Override annotation prevents you from making such errors.

You should be in the habit of using @Override whenever you override a superclass method.

2) @Deprecated Annotation:

This annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example.

3) @SuppressWarnings Annotation: 

Just tell compiler, please don’t shout. I know what I’m doing.

4) @Retention Annotation:

The retention annotation indicates where and how long annotations with this type are to be retained. There are three values:

  • RetentionPolicy.SOURCE—Annotations with this type will be by retained only at the source level and will be ignored by the compiler.
  • RetentionPolicy.CLASS—Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM.
  • RetentionPolicy.RUNTIME—Annotations with this type will be retained by the VM so they can be read only at run-time.

In this example, the @Retention(RetentionPolicy.RUNTIME) annotation indicates that your Crunchify_Retention annotation is to be retained by the VM so that it can be read reflectively at run-time.

5) @Documented Annotation:

Now try to run Java Doc command and see output.

6) @Target Annotation:

Target indicates which program element(s) can be annotated using instances of the annotated annotation type. The value of Target is one of the members of the java.lang.annotation.ElementType enum:

  1. ANNOTATION_TYPE. The annotated annotation type can be used to annotate annotation type declaration.
  2. CONSTRUCTOR. The annotated annotation type can be used to annotate constructor declaration.
  3. FIELD. The annotated annotation type can be used to annotate field declaration.
  4. LOCAL_VARIABLE. The annotated annotation type can be used to annotate local variable declaration.
  5. METHOD. The annotated annotation type can be used to annotate method declaration.
  6. PACKAGE. The annotated annotation type can be used to annotate package declarations.
  7. PARAMETER. The annotated annotation type can be used to annotate parameter declarations.
  8. TYPE. The annotated annotation type can be used to annotate type declarations.

7) @Inherited Annotation:

Exactly as the name sounds, an @Inherited annotation type is inherited by subclasses of an annotated type.

In this example, Superclass has been explicitly annotated with both @ForEveryone and @JustForMeSubclass hasn’t been explicitly marked with either one; however, it inherits @ForEveryone because the latter is annotated with @Inherited@JustForMe isn’t annotated, so it isn’t inherited by Subclass.

References:

http://en.wikibooks.org/wiki/Java_Programming/Annotations/Meta-Annotations

http://www.developer.com/java/other/article.php/10936_3556176_3/An-Introduction-to-Java-Annotations.htm

http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html

If you enjoyed this post, help spread knowledge & subscribe to Crunchify's RSS feed.
twitter Understanding Java Annotation   Annotation Examplesfacebook Understanding Java Annotation   Annotation Examplesgoogle Understanding Java Annotation   Annotation Examplespinterest Understanding Java Annotation   Annotation Examplestumblr Understanding Java Annotation   Annotation Examplesbuffer Understanding Java Annotation   Annotation Examples

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more!