Java Reference
In-Depth Information
TIP: (continued)
One word of warning: This only works for static methods. This does not in any way
apply to ordinary nonstatic methods.
to
sentence = toUpperCase(firstCharacter)
+ sentence.substring(1);
extra code
on CD
The program StaticImportDemo.java on the accompanying CD 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 follow-
ing import statements at the beginning of the program file:
static import
of constants
import static java.lang.Math.PI;
or
import static java.lang.Math.*;
The program StaticImportDemo2.java on the accompanying CD redoes the program
in Display 5.7 using a static import statement in this way.
extra code
on CD
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 sec-
tion until you have had more practice defining and using classes. You do not need the
material in this section before doing Section 5.4.
Search WWH ::




Custom Search