@Override – Checks that the method is an override. Causes a compile error if the method is not found in one of the parent classes or implemented interfaces.

The first main distinction between kinds of annotation is whether they’re used at compile time and then discarded
 (like @Override) or placed in the compiled class file and available at runtime (like Spring’s @Component).
This is determined by the @Retention policy of the annotation. If you’re writing your own annotation,
you’d need to decide whether the annotation is helpful at runtime (for autoconfiguration, perhaps) or
only at compile time (for checking or code generation).When compiling code with annotations, the compiler sees the annotation just like it sees other modifiers on source
elements, like access modifiers (public/private) or final. When it encounters an annotation, it runs an annotation
processor, which is like a plug-in class that says it’s interested a specific annotation. The annotation processor
 generally uses the Reflection API to inspect the elements being compiled and may simply run checks on them,
 modify them, or generate new code to be compiled. @Override is an example of the first; it uses the Reflection API
to make sure it can find a match for the method signature in one of the superclasses and uses the Messager to
cause a compile error if it can’t.
  • Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.
  • Suppose that a software group traditionally starts the body of every class with comments providing important information:
    public class Generation3List extends Generation2List {
       // Author: John Doe
       // Date: 3/17/2002
       // Current revision: 6
       // Last modified: 4/12/2004
       // By: Jane Doe
       // Reviewers: Alice, Bill, Cindy
       // class code goes here

    To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:

    @interface ClassPreamble {
       String author();
       String date();
       int currentRevision() default 1;
       String lastModified() default "N/A";
       String lastModifiedBy() default "N/A";
       // Note use of array
       String[] reviewers();

    The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.

    The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.

    After the annotation type is defined, you can use annotations of that type, with the values filled in, like this:

    @ClassPreamble (
       author = "John Doe",
       date = "3/17/2002",
       currentRevision = 6,
       lastModified = "4/12/2004",
       lastModifiedBy = "Jane Doe",
       // Note array notation
       reviewers = {"Alice", "Bob", "Cindy"}
    public class Generation3List extends Generation2List {
    // class code goes here
  • Type annotations were created to support improved analysis of Java programs way of ensuring stronger type checking. e.g., @NonNull String str;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s