Java Reference
In-Depth Information
Another advantage is simpler naming of class names in your code. If a class is
part of a package, other classes in the package can refer to it using only its short
name instead of its fully qualified name. For example, any class in the
java4cobol.message
package can refer to
ErrorMsg
as simply
ErrorMsg
instead of the
more precise
java4cobol.message.ErrorMsg
.
A class from one package can still refer to classes in another package by
using the fully qualified name. To continue the example, an
Account
class in the
general_ledger
package could refer to the
ErrorMsg
class by its
package.class-
name: java4cobol.message.ErrorMsg
.
package general_ledger;
public class Accounts {
public String accountID;
public BigDecimal accountBalance;
...
// Create a new ErrorMsg object. Use the complete package name and class
// name.
java4cobol.message.ErrorMsg accountNotFound =
new java4cobol.message.ErrorMsg ();
...
}
However, this is more typing than the average programmer is willing to do. So,
Java has defined a way for you to announce which classes your class will use, called
the
import
statement, which must follow any
package
statements in your program.
It tells the compiler that your class might refer to the classes identified in the import
statement. The full class name is described only once in the import statement; af-
terward, the compiler knows to use the full name whenever it sees the short name.
package general_ledger;
import java4cobol.message.ErrorMsg;
public class Accounts {
public String accountID;
public BigDecimal accountBalance;
...
// Create a new ErrorMsg object. No need to use the full class name or FQN.
ErrorMsg accountNotFound = new ErrorMsg ();
...
}