From the course: Java SE 11 Developer (1Z0-819) Cert Prep

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Target, retention, and inspection

Target, retention, and inspection - Java Tutorial

From the course: Java SE 11 Developer (1Z0-819) Cert Prep

Target, retention, and inspection

- Now let's look at target retention and inspection of annotations. By default, an annotation can be applied to almost any syntactic element of our source code, which is not usually helpful. So it's possible and in fact, normal, to restrict an annotation to being used on specific syntax elements. And this is done rather amusingly by annotating the annotation. So here's an example. There's our basic declaration for the annotation with the @interface piece, and you'll notice that we have annotated this annotation with the @target annotation. That takes an array of ElementTypes, and ElementTypes are things like constructor and method and so forth. We'll look at the long list in a moment or two. This array allows us to specify what the legal places that we can use this annotation actually are. Here's another example. You'll notice in this case, we do not have the curly braces surrounding the array. It's a perhaps, rather odd feature of the annotation mechanism in general that if the type…

Contents