Java Reference
In-Depth Information
TIP: (continued)
sentence = toUpperCase(firstCharacter)
+ sentence.substring(1);
One word of warning: This works for static methods only. This does not in any way
apply to ordinary nonstatic methods.
The program
StaticImportDemo.java
on the accompanying website redoes the
program in Display 5.9 using a static
import
statement in this way.
You can, of course, do this with
Character
replaced by any other class, and then
you can use static methods in that class without the need to preface the method name
with the class name and a dot.
You can use static
import
statements for constants as well as methods. If you want
to use
PI
instead of
Math.PI
in the program in Display 5.7, just include one of the
following
import
statements at the beginning of the program fi le:
extra code
on website
static
import
of constants
import static
java.lang.Math.PI;
or
import static
java.lang.Math.*;
extra code
on website
The program
StaticImportDemo2.java
on the accompanying website redoes the
program in Display 5.7 using a static
import
statement in this way.
■
5.3
Using and Misusing References
Loose lips sink ships.
MILITARY SLOGAN
Just as a military campaign requires constant vigilance to ensure that its plans are kept
secret, so your programming requires constant vigilance to ensure that private instance
variables remain truly private. As we will see, adding the
private
modifier before
instance variable declarations is not always all that you need to do. There can be privacy
leaks in a poorly designed class just as there can be privacy leaks in a military campaign.
The material in this section is important but more subtle and harder to digest than
the material we have seen before now. If you want, you may postpone reading this
section until you have had more practice defining and using classes. You do not need
the material in this section to understand Section 5.4 .