How to describe Java code with annotations

You have possibly encountered predicaments exactly where you need to have to affiliate metadata (information that describes other information) with classes, procedures, and/or other software features. For illustration, your programming staff may well need to have to discover unfinished classes in a massive software. For each and every unfinished class, the metadata would probable involve the name of the developer dependable for finishing the class and the class’s envisioned completion day.

Before Java five, responses ended up the only versatile mechanism that Java experienced to supply for associating metadata with software features. Nonetheless, responses are a weak decision. Simply because the compiler ignores them, responses are not obtainable at runtime. And even if they ended up obtainable, the textual content would have to be parsed to get hold of crucial information products. Without the need of standardizing how the information products are specified, these information products may well verify unachievable to parse.


Down load the supply code for examples in this Java one zero one tutorial. Established by Jeff Friesen for InfoWorld.

Java five modified every thing by introducing annotations, a common mechanism for associating metadata with many software features. This mechanism consists of four parts:

  • An @interface mechanism for declaring annotation types.
  • Meta-annotation types, which you can use to discover the software features to which an annotation form applies to discover the life time of an annotation (an instance of an annotation form) and a lot more.
  • Support for annotation processing by using an extension to the Java Reflection API (to be reviewed in a upcoming report), which you can use to find out a program’s runtime annotations, and a generalized device for processing annotations.
  • Typical annotation types.

I’ll describe how to use these parts as we function our way via this report.

Declaring annotation types with @interface

You can declare an annotation form by specifying the @ symbol immediately adopted by the interface reserved phrase and an identifier. For illustration, Listing one declares a uncomplicated annotation form that you may well use to annotate thread-harmless code.

Listing one:

Next Post

20 Xbox Series X Games Revealed (Trailers): Every Game Shown

The summer season of pre-recorded E3-kind-situations proceeds! Sony told us all about the PS5’s video games some months again, and now Microsoft’s Xbox Showcase just wrapped up, bringing a handful of new video games and a couple Xbox Sequence X exclusives—some of which are coming out this 12 months. As […]

Subscribe US Now