Java Reference
In-Depth Information
public
public static
throws
IOException
{
String
[]
names
=
getPackageContent
(
"com.darwinsys.io"
);
for
static
void
void
main
(
String
[]
args
)
throws
for
(
String name
:
names
) {
System
.
out
.
println
(
name
);
}
System
.
out
.
println
(
"Done"
);
}
}
Note that if you run this application in the “javasrc” project, it will list the members of the
demonstration package (
com.darwinsys.io
) twice, because it will find them both in the
build directory and in the JAR file. If this is an issue, change the List to a Set (see
The Col-
Using and Defining Annotations
Problem
You need to know how to use annotations in code or to define your own annotations.
Solution
Apply annotations in your code using
@
AnnotationName
before a class, method, field, etc.
Define annotations with
@interface
at the same level as
class
,
interface
, etc.
Discussion
Annotations are a way of adding additional information beyond what the source code con-
veys. Annotations may be directed at the compiler or at runtime examination. Their syntax
was somewhat patterned after javadoc annotations (such as
@author
,
@version
inside “doc
comments”). Annotations are what I call
class-like things
(so they have initial-cap names),
but are prefixed by
@
sign where used (e.g.,
@Override
). You can place them on classes,
methods, fields, and a few other places; they must appear immediately before what they an-
notate (ignoring space and comments). A given annotation may only appear once in a given
position (this is relaxed in Java 8 or 9).