This brings in the entire utility library thats part of the standard Java distribution. For instance, theres a class called ArrayList in java.util, so you can now either specify the full name java.util.ArrayList (which you can do without the import statement), or you can simply say ArrayList (because of the import). Feedback
If you want to bring in a single class, you can name that class in the import statement
import java.util.ArrayList;
Now you can use ArrayList with no qualification. However, none of the other classes in java.util are available. Feedback
The reason for all this importing is to provide a mechanism to manage name spaces. The names of all your class members are insulated from each other. A method f( ) inside a class A will not clash with an f( ) that has the same signature (argument list) in class B. But what about the class names? Suppose you create a Stack class that is installed on a machine that already has a Stack class thats written by someone else? This potential clashing of names is why its important to have complete control over the name spaces in Java, and to be able to create a completely unique name regardless of the constraints of the Internet. Feedback
Most of the examples thus far in this book have existed in a single file and have been designed for local use, so they havent bothered with package names. (In this case the class name is placed in the default package.) This is certainly an option, and for simplicitys sake this approach will be used whenever possible throughout the rest of this book. However, if youre planning to create libraries or programs that are friendly to other Java programs on the same machine, you must think about preventing class name clashes. Feedback
When you create a source-code file for Java, its commonly called a compilation unit (sometimes a translation unit). Each compilation unit must have a name ending in .java, and inside the compilation unit there can be a public class that must have the same name as the file (including capitalization, but excluding the .java filename extension). There can be only one public class in each compilation unit, otherwise the compiler will complain. If there are additional classes in that compilation unit, they are hidden from the world outside that package because theyre not public, and they comprise support classes for the main public class. Feedback
When you compile a .java file, you get an output file for each class in the .java file. Each output file has the name of a class in the .java file, but with an extension of .class. Thus you can end up with quite a few .class files from a small number of .java files. If youve programmed with a compiled language, you might be used to the compiler spitting out an intermediate form (usually an obj file) that is then packaged together with others of its kind using a linker (to create an executable file) or a librarian (to create a library). Thats not how Java works. A working program is a bunch of .class files, which can be packaged and compressed into a Java ARchive (JAR) file (using Javas jar archiver). The Java interpreter is responsible for finding, loading, and interpreting[26] these files. Feedback
A library is a group of these class files. Each file has one class that is public (youre not forced to have a public class, but its typical), so theres one component for each file. If you want to say that all these components (each in their own separate .java and .class files) belong together, thats where the package keyword comes in. Feedback
When you say:
package mypackage;
at the beginning of a file (if you use a package statement, it must appear as the first noncomment in the file), youre stating that this compilation unit is part of a library named mypackage. Or, put another way, youre saying that the public class name within this compilation unit is under the umbrella of the name mypackage, and anyone who wants to use the name must either fully specify the name or use the import keyword in combination with mypackage (using the choices given previously). Note that the convention for Java package names is to use all lowercase letters, even for intermediate words. Feedback
For example, suppose the name of the file is MyClass.java. This means there can be one and only one public class in that file, and the name of that class must be MyClass (including the capitalization):
package mypackage;
public class MyClass {
// . . .Now, if someone wants to use MyClass or, for that matter, any of the other public classes in mypackage, they must use the import keyword to make the name or names in mypackage available. The alternative is to give the fully qualified name:
mypackage.MyClass m = new mypackage.MyClass();
The import keyword can make this much cleaner:
import mypackage.*; // . . . MyClass m = new MyClass();
Its worth keeping in mind that what the package and import keywords allow you to do, as a library designer, is to divide up the single global name space so you wont have clashing names, no matter how many people get on the Internet and start writing classes in Java. Feedback
You might observe that, since a package never really gets packaged into a single file, a package could be made up of many .class files, and things could get a bit cluttered. To prevent this, a logical thing to do is to place all the .class files for a particular package into a single directory; that is, use the hierarchical file structure of the operating system to your advantage. This is one way that Java references the problem of clutter; youll see the other way later when the jar utility is introduced. Feedback
Collecting the package files into a single subdirectory solves two other problems: creating unique package names, and finding those classes that might be buried in a directory structure someplace. This is accomplished, as was introduced in Chapter 2, by encoding the path of the location of the .class file into the name of the package. By convention, the first part of the package name is the reversed Internet domain name of the creator of the class. Since Internet domain names are guaranteed to be unique, if you follow this convention, your package name will be unique and youll never have a name clash. (That is, until you lose the domain name to someone else who starts writing Java code with the same path names as you did.) Of course, if you dont have your own domain name, then you must fabricate an unlikely combination (such as your first and last name) to create unique package names. If youve decided to start publishing Java code, its worth the relatively small effort to get a domain name. Feedback
The second part of this trick is resolving the package name into a directory on your machine, so when the Java program runs and it needs to load the .class file (which it does dynamically, at the point in the program where it needs to create an object of that particular class, or the first time you access a static member of the class), it can locate the directory where the .class file resides. Feedback
The Java interpreter proceeds as follows. First, it finds the environment variable CLASSPATH[27] (set via the operating system, and sometimes by the installation program that installs Java or a Java-based tool on your machine). CLASSPATH contains one or more directories that are used as roots in a search for .class files. Starting at that root, the interpreter will take the package name and replace each dot with a slash to generate a path name from the CLASSPATH root (so package foo.bar.baz becomes foo\bar\baz or foo/bar/baz or possibly something else, depending on your operating system). This is then concatenated to the various entries in the CLASSPATH. Thats where it looks for the .class file with the name corresponding to the class youre trying to create. (It also searches some standard directories relative to where the Java interpreter resides). Feedback
To understand this, consider my domain name, which is bruceeckel.com. By reversing this, com.bruceeckel establishes my unique global name for my classes. (The com, edu, org, etc., extensions were formerly capitalized in Java packages, but this was changed in Java 2 so the entire package name is lowercase.) I can further subdivide this by deciding that I want to create a library named simple, so Ill end up with a package name:
package com.bruceeckel.simple;
Now this package name can be used as an umbrella name space for the following two files: Feedback