O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.9. Understanding Packages

NOTE

Packages are fundamental to Java programs so make sure you understand this section.

Packages are implicit in the organization of the standard classes as well as your own programs, and they influence the names you can use for classes and the variables and methods they contain. Essentially, a package is a uniquely named collection of classes. The primary reason for grouping classes in packages is to avoid possible name clashes with your own classes when you are using prewritten classes in an application. The names used for classes in one package will not interfere with the names of classes in another package or your program because the class names in a package are all qualified by the package name. Thus, the String class you have been using is in the java.lang so the full name of the class is java.lang.String. You have been able to use the unqualified name because all the classes in the java.lang package are always available in your program code; there's an implicit import statement in effect for all the names in the java.lang package. If you happened to have defined a class of your own with the name String, using the name String would refer to your class, but you could still use the library class that has the same name by using its full name in your code, java.lang.String.

Every class in Java is contained in a package, including all those you have defined in the examples. You haven't seen many references to package names so far because you have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required