Java Fundamentals  «Prev  Next»


Lesson 3Java Packages
Objective Describe how packages are declared and imported.

Declare Java Packages to be imported

You can use a package to group together a related set of classes and interfaces. Packages also provide access protection and namespace management. You can create separate packages to define classes for separate projects, such as Android games and online healthcare systems. Further, you can create subpackages within these packages, such as separate subpackages for GUIs, database access, networking, and so on.
Note: In real-life projects, you will rarely work with a package-less class or interface. Almost all organizations that develop software have strict package naming rules, which are often documented. All classes and interfaces are defined in a package. If you do not include an explicit package statement in a class or an interface, it is part of a default package.
  • Java Packages:
    The Java API is organized by packages . Each package contains related classes and interfaces. A package is used to define a separate naming context. Packages allow multiple classes and interfaces to have the same name. Classes and interfaces with the same name are defined in separate packages. Suppose that both you and I want to define a class named MyClass. I can define my class in a package named myPackage and you can define your class in a package named yourPackage.
    A package naming convention can help assure that you are creating a unique package name. The package statement is used to identify the package with which the classes and interfaces of a source code file should be associated. Its syntax is as follows:
    package packageName;
    If a package statement is included in a source code file, it must appear as the first non-blank, non-comment line in the file. If a source code file does not have a package statement, then the file's classes and interfaces are put in the default (no name) package.

Selecting Package Names

In an effort to avoid name collisions, Sun recommends using a package naming scheme intended to produce unique package names.
This scheme is based on inverting your Internet domain name. For example, a package named tools developed at acmecorp.com would have a full package name of com.acmecorp.tools. While some companies are using this naming scheme, others are simply using their company name to produce package names such as acmecorp.tools. Keep in mind that you are free to use any package naming scheme that you like.
Naming Conventions Package names are written in all lower case to avoid conflict with the names of classes or interfaces.Companies use their reversed Internet domain name to begin their package names for example,
com.java.security

for a package named mypackage created by a programmer at javadeploy.com.
Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage). Packages in the Java language itself begin with java. or javax.
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore.


Importing from other Packages

All classes and interfaces of the same package can be referenced without having to identify their package name. However, to reference the classes and interfaces of other packages, you either need to All classes and interfaces of the same package can be referenced without having to identify their package name. However, to reference the classes and interfaces of other packages, you either need to prepend their package name, or import them using the import statement. Its syntax takes the following three forms:
 
import packageName.*;
import packageName.className;
import packageName.interfaceName;

The first form imports all classes and interfaces of the package named packageName. The other forms are used to import specific classes and interfaces.

Prepending Package Name

The classes and interfaces of other packages can be accessed by prepending their package name. For example, to access the Vector class of the java.util package, you could refer to it as java.util.Vector as in the following example:
A package is a collection of classes with a related purpose and all classes in the standard library are contained in packages. The Rectangle class belongs to the package java.awt (where awt is an abbreviation for "Abstract Windowing Toolkit"), which contains many classes for drawing windows and graphical shapes.
Java classes are grouped into packages. Use the import statement to use classes that are defined in other packages.
java.util.Vector v = new java.util.Vector();

The problem with using the package name is that it adds a lot more text to your code. I recommend that you import the java.util package (at the top of the file) instead of prepending java.util whenever you reference a class or interface of this package

There is no need to import the java.lang package. Since it contains classes and interfaces that are fundamental to all Java programs, it is always imported by default.

Packages Compilation Units - Quiz

Click the Quiz link below to check your understanding of packages and compilation units.
Packages Compilation Units - Quiz

SEMrush Software