0

Possible Duplicate:
What function do these Java annotations serve?
How and where are Annotations used in Java?

Well I'm trying to figure out, what annotations are, read plenty of docs, but still don't understand what they do and how to use it... Well I have this sample annotation description:

    @Documented
@interface ClassPreamble {
   String author();
   int currentRevision() default 1;
   String lastModified() default "N/A";
   String lastModifiedBy() default "N/A";
}

And then I'm trying to use it, but I don't even know what to expect, lol:

   @ClassPreamble (author = "James")
public class PreAmbleReal {
    static public void main(String[] args) {




    }

    public String author();

}

So, can anyone explain this to me? Otherwise I'll have wasted a few hours of time and learn nothing. :/

Community
  • 1
  • 1
Arturas M
  • 4,120
  • 18
  • 50
  • 80

2 Answers2

1

One of the uses of annotations is that it tells the compiler to check for specific things. For example, @Override would make sure that the function is actually the same as the base class. There are some other annotation like @Author. I am certain you've been to this page but this is a much more detailed explanation - http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

FSP
  • 4,677
  • 2
  • 19
  • 19
1

Annotations don't do anything by themselves. They are some attributes which are put on some other code structs (such as methods, classes, arguments,...). There must be some other processor to read the annotations and do something.

Some annotations are just in source codes, and are not put in the compiled code, so tools which process the source code read them and process them, some annotations are put in compiled code but are not loaded by class loader, and some are put in compiled code and are loaded by class loader, so they can be read in runtime (these 3 behaviors are distinguished by @Retention annotation on the annotation itself).

As far as I know in Java Language Specification, it is mentioned that lack of annotation classes on runtime shall not stop the application working, it means the class-loader just to ignore them. But may be the processor, or framework which use them make errors (which is what they should do).

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69