System, but they may not be reproduced for publication



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

ax  + bx + c = 0
The program uses two static methods from Java’s built­in math class Math, which is
part of java.lang. The first is Math.pow( ), which returns a value raised to a specified
power. The second is Math.sqrt( ), which returns the square root of its argument.
2


Because pow( ) and sqrt( ) are static methods, they must be called through the use of
their class’ name, Math. This results in a somewhat unwieldy expression:
Furthermore, having to specify the class name each time pow( ) or sqrt( ) (or any of
Java’s other math methods, such as sin( ), cos( ), and tan( )) are used can become
tedious.
You can eliminate the tedium of specifying the class name through the use of static
import, as shown in the following version of the preceding program:


In this version, the names sqrt and pow are brought into view by these static import
statements:
After these statements, it is no longer necessary to qualify sqrt( ) or pow( ) with its
class name. Therefore, the expression can more conveniently be specified, as shown
here:
As you can see, this form is considerably shorter and easier to read.
There are two general forms of the import static statement. The first, which is used by
the preceding example, brings into view a single name. Its general form is shown here:
import static pkg.type­name.static­member­name;
Here, type­name is the name of a class or interface that contains the desired static
member. Its full package name is specified by pkg. The name of the member is specified


by static­member­name.
The second form of static import imports all static members. Its general form is shown
here:
import static pkg.type­name.*;
If you will be using many static methods or fields defined by a class, then this form lets
you bring them into view without having to specify each individually. Therefore, the
preceding program could have used this single import statement to bring both pow( )
and sqrt( ) (and all other static members of Math) into view:
Of course, static import is not limited just to the Math class or just to methods. For
example, this brings the static field System.out into view:
After this statement, you can output to the console without having to qualify out with
System, as shown here:
Whether importing System.out as just shown is a good idea is subject to debate.
Although it does shorten the statement, it is no longer instantly clear to anyone reading
the program that the out being referred to is System.out.
As convenient as static import can be, it is important not to abuse it. Remember, one
reason that Java organizes its libraries into packages is to avoid namespace collisions.
When you import static members, you are bringing those members into the current
namespace. Thus, you are increasing the potential for namespace conflicts and
inadvertent name hiding. If you are using a static member once or twice in the
program, it’s best not to import it. Also, some static names, such as System.out, are so
recognizable that you might not want to import them. Static import is designed for
those situations in which you are using a static member repeatedly, such as when
performing a series of mathematical computations. In essence, you should use, but not
abuse, this feature.
Ask the Expert


Q
: Using static import, can I import the static members of classes that
I create?
A
: Yes, you can use static import to import the static members of classes and
interfaces you create. Doing so is especially convenient when you define several
static members that are used frequently throughout a large program. For example,
if a class defines a number of static final constants that define various limits, then
using static import to bring them into view will save you a lot of tedious typing.
ANNOTATIONS (METADATA)
Java provides a feature that enables you to embed supplemental information into a
source file. This information, called an annotation, does not change the actions of a
program. However, this information can be used by various tools, during both
development and deployment. For example, an annotation might be processed by a
source­code generator, by the compiler, or by a deployment tool. The term metadata is
also used to refer to this feature, but the term annotation is the most descriptive, and
more commonly used.
Annotation is a large and sophisticated topic, and it is far beyond the scope of this book
to cover it in detail. However, an overview is given here so that you will be familiar with
the concept.

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   50   51   52   53   54   55   56   57   ...   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ə