img
As convenient as static import can be, it is important not to abuse it. Remember, the 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 global namespace. Thus,
you are increasing the potential for namespace conflicts and for the inadvertent hiding of
other names. 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.
Invoking Overloaded Constructors Through this( )
When working with overloaded constructors, it is sometimes useful for one constructor to
invoke another. In Java, this is accomplished by using another form of the this keyword.
The general form is shown here:
this(arg-list)
When this( ) is executed, the overloaded constructor that matches the parameter list
specified by arg-list is executed first. Then, if there are any statements inside the original
constructor, they are executed. The call to this( ) must be the first statement within the
constructor.
To understand how this( ) can be used, let's work through a short example. First,
consider the following class that does not use this( ):
class MyClass {
int a;
int b;
// initialize a and b individually
MyClass(int i, int j) {
a = i;
b = j;
}
// initialize a and b to the same value
MyClass(int i) {
a = i;
b = i;
}
// give a and b default values of 0
MyClass( ) {
a = 0;
b = 0;
}
}
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home