Java Reference
In-Depth Information
Knowing just the package name is not enough to locate a class file, because it specifies only a partial path to the
class file. The leading part of the class file path on the file system is obtained from an environment variable called
CLASSPATH . I will discuss CLASSPATH in detail shortly.
The package declaration is optional. What repository does your Java program belong to if you omit the package
declaration? A Java program (strictly speaking, a Java type), which does not have a package declaration, is said to
be part of an unnamed package (also called default package). I will discuss unnamed package in more detail in the
sections to follow.
Java source code is case sensitive. The keyword package has to be written as is—in all lowercase. The word
Package or packAge cannot replace the keyword package . The package name is also case sensitive. On some operating
systems, the names of files and directories are case sensitive. On those systems, the package names will be case
sensitive, as you have seen: the package name is treated as a directory name on the host system. The package names
com.jdojo.intro and Com.jdojo.intro may not be the same depending on the host system that you are working on.
It is recommended to use package names in all lowercase.
The package declaration is a simple and important part of Java source code. It is recommended that you always
use a package declaration in your source code. Typically, a package name starts with a reverse domain name of the
company, such as com.yahoo for Yahoo, com.google for Google etc. Using the reverse domain name of the company
as the leading part of the package name guarantees that a package name will not conflict with package names used
by other companies, provided they follow the same guidelines. If you do not own a domain name, make up one that is
likely to be unique. This is just a guideline. There is nothing in practice that guarantees a unique package name for all
Java programs written in the world.
Import Declarations
Import declarations in Java source code are optional. You may develop a Java application without using even a
single import declaration. Why is an import declaration needed at all? Using import declarations in your code makes
your life easier. It saves you some typing and makes your code cleaner and easier to read. In an import declaration,
you tell the Java compiler that you may use one or more classes from a particular package. Whenever a type (a class,
an interface, or an enum) is used in Java source code, it must be referred to by its fully qualified name. Using an
import declaration for a type lets you refer to a type using its simple name. I will discuss simple and fully qualified
names of a type shortly.
Unlike a package declaration, there is no restriction on the number of import declarations in the source code.
The following are two import declarations:
import com.jdojo.intro.Account;
import com.jdojo.util.*;
I will discuss import declarations in detail in the chapter on classes and objects. In this section, I will discuss only
the meaning of all parts of an import declaration. An import declaration starts with the keyword import . The second
part in an import declaration consists of two parts:
A package name from which you want to use the classes in the current source code
A class name or an asterisk (*) to indicate that you may use one or more of the classes stored in
the package.
Finally, an import declaration ends with a semicolon. The above two import declarations state the following:
Account by its simple name from com.jdojo.intro package.
We may use a class named
enum s by their simple names from the com.jdojo.util
We may use any classes, interfaces, and
package.
 
Search WWH ::




Custom Search