2

As I was trying to sharpen my Java skills by creating a Minecraft server plugin, I came across a @EventHandler in a code example for Bukkit plugin development. This is was put just over a method implementation and I was wondering what it is called.

I remember having seen some @Override somewhere else and I want to learn what it is called so I can search it online...

Makoto
  • 104,088
  • 27
  • 192
  • 230
hephaestuz
  • 123
  • 1
  • 5
  • 1
    possible duplicate of [How and where are Annotations used in Java?](http://stackoverflow.com/questions/1372876/how-and-where-are-annotations-used-in-java) – Brian Roach Jun 29 '13 at 17:16

5 Answers5

6

This thing is called an annotation. In Python the counterpart of Java annotations are called decorators.

Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32
  • Decorators, however, do not quite play the same role; decorators are pretty much Java's `RetentionPolicy.RUNTIME` annotations with a direct language injection. Java doesn't work this way. – fge Jun 29 '13 at 18:34
6

it is Annotation.

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.

From the doc the usage of annotations are mainly

  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 3
    Well, they _can_ have a direct effect... All depends on their retention policy and what the code dealing with them actually does – fge Jun 29 '13 at 17:12
  • 1
    @fge: yes, for instance EJB annotations introducing interceptors do affect program behavior. – Alexander Serebrenik Jun 29 '13 at 17:14
  • @fge: I think the docs is talking about the direct effect on the annotated code not the code that reads the annotation. – Bhesh Gurung Jun 29 '13 at 18:02
  • @BheshGurung which is, imho, a mishap, since annotations have no real value unless they are actually processed – fge Jun 29 '13 at 18:32
2

OK, I will not replicate the link to what an annotation is. Rather, I will talk about how they can operate.

Annotations have both a Target and a Retention; optionally, they can also be @Documented so that you know this annotation has been present at some time in your source code. Now, onto the target and retention since these two "meta-annotations" will definitely have an influence on what you can expect:

  • the target determines what that annotation can be applied to; this can be a whole class, an instance variable, a method, a... Well, see the doc.
  • the retention determines how "long" this annotation persists in your source code. The two retention policies used in majority are SOURCE and RUNTIME.

In an annotation such as yours, it is typically an annotation with a runtime retention policy. Such an annotation can be used, at runtime, by specific processing code, to change the behaviour of the target so that its behaviour be controlled by this processing code. Such code is logically called an annotation processor.

Annotations have begun to take quite some an importance in some JSRs:

  • JSR 330 defines @Inject, @Provider etc as runtime annotations, so that frameworks willing to do dependency injection can rely on these annotations being present; this is the case, for instance, for dependency injection frameworks such as Dagger and Guice (since version 3.0);
  • JSR 305 defines @Immutable, @ThreadSafe, @NotThreadSafe, @Nullable, @Nonnull with a source retention policy, and @Documented; these annotations can be used by static code analysis tools, and of equal importance, they are typically @Documented as well. Therefore you know what to expect of them.
fge
  • 119,121
  • 33
  • 254
  • 329
0

It is just a annotation.

@Override for ie is used in methods that is overriding method with the same name in the extended "super class"

0

It's an annotation.

While it usually has no direct effect on code, some object databases may use it to specify the behavior of one or more fields in regard to indexing, keying, or other functions.

nanofarad
  • 40,330
  • 4
  • 86
  • 117