Annotation parameters

Annotations, as we saw previously, can have parameters. To declare these parameters in the @interface declaration of the annotation, we use methods. These methods have a name and a return value, but they should not have an argument. You may try to declare some parameters, but the Java compiler will be strict and will not compile your code.

The values can be defined at the place where the annotation is used, using the name of the method and with the = character, assigning to them a value that is compatible with the method type. For example, let's suppose that we modify the declaration of the PoweredDevice annotation to the following:

public @interface ParameteredPoweredDevice { 
    String myParameter(); 
}

In such a case, ...

Get Java Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.