Java Reference
In-Depth Information
public void payEmployee(Hourly h)
{
h.computePay();
}
}
Importing each class individually is obviously more typing, but this option
is actually preferred over using the import statement with the wild card. There
are some rare instances where importing the wild card either does not work or
causes a naming conflict. These situations will not arise in anything we do in
this topic, however, so I will use the wild card import statement regularly.
There is no size or performance benefit gained from importing classes
individually versus importing an entire package. The compiled bytecode
will look the exact same because the compiler removes your import
statements and replaces all class names with their fully qualified name.
We have been using classes such as String, System, Math, and Object
throughout the examples so far in this topic. Why did we not have to use the
import keyword? Because each of these classes is in the java.lang package. The
java.lang package is implicitly imported in every source code file.
In fact, you can import java.lang if you like, as the following class does, but
it is never necessary to do so:
package electronics;
import java.lang.*;
public class Radio extends Object
{
public String make, model;
}
The Radio class imports java.lang.* explicitly. The Radio class uses both
Object and String from java.lang, but the code will compile just fine without
the import statement.
The Directory Structure of Packages
Two major results occur when a class is placed in a package:
The name of the package becomes a part of the name of the class, as we
just discussed in the previous section.
■■
The name of the package must match the directory structure where the
corresponding bytecode resides.
■■
Search WWH ::




Custom Search