System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə55/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   51   52   53   54   55   56   57   58   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

NOTE
A more detailed discussion of annotations can be found in my book Java: The
Complete Reference, Eleventh Edition (Oracle Press/McGraw­Hill Education, 2019).
An annotation is created through a mechanism based on the interface. Here is a
simple example:


This declares an annotation called MyAnno. Notice the @ that precedes the keyword
interface. This tells the compiler that an annotation type is being declared. Next,
notice the two members str( ) and val( ). All annotations consist solely of method
declarations. However, you don’t provide bodies for these methods. Instead, Java
implements these methods. Moreover, the methods act much like fields.
All annotation types automatically extend the Annotation interface. Thus,
Annotation is a super­interface of all annotations. It is declared within the
java.lang.annotation package.
Originally, annotations were used to annotate only declarations. In this usage, any type
of declaration can have an annotation associated with it. For example, classes, methods,
fields, parameters, and enum constants can be annotated. Even an annotation can be
annotated. In such cases, the annotation precedes the rest of the declaration. Beginning
with JDK 8, you can also annotate a type use, such as a cast or a method return type.
When you apply an annotation, you give values to its members. For example, here is an
example of MyAnno being applied to a method:
This annotation is linked with the method myMeth( ). Look closely at the annotation
syntax. The name of the annotation, preceded by an @, is followed by a parenthesized
list of member initializations. To give a member a value, that member’s name is
assigned a value. Therefore, in the example, the string "Annotation Example" is
assigned to the str member of MyAnno. Notice that no parentheses follow str in this
assignment. When an annotation member is given a value, only its name is used. Thus,
annotation members look like fields in this context.
Annotations that don’t have parameters are called marker annotations. These are
specified without passing any arguments and without using parentheses. Their sole
purpose is to mark an item with some attribute.
Java defines many built­in annotations. Most are specialized, but nine are general
purpose. Four are imported from java.lang.annotation: @Retention,
@Documented, @Target, and @Inherited. Five, @Override, @Deprecated,
@SafeVarargs, @FunctionalInterface, and @SuppressWarnings, are included
in java.lang. These are shown in 
Table 12­1
.


Table 12­1 The General Purpose Built­in Annotations
NOTE
Beginning with JDK 8, java.lang.annotation also includes the annotations
@Repeatable and @Native. @Repeatable supports repeatable annotations, which
are annotations that can be applied more than once to a single item. @Native is used
to annotate a constant field accessed by executable (i.e., native) code. Both are special­
use annotations that are beyond the scope of this book.
Here is an example that uses @Deprecated to mark the MyClass class and the
getMsg( ) method. When you try to compile this program, warnings will report the use
of these deprecated elements.


As a point of interest, over the years several elements in Java’s API library have been
deprecated, and additional deprecations may occur as Java continues to evolve.
Remember, although deprecated API elements are still available, they are not
recommended for use. Typically, an alternative to the deprecated API element is
offered.

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   51   52   53   54   55   56   57   58   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə