Packages
- Packages allow related classed to be
grouped together, and help
prevent namespace collisions among classes in a large project.
- Classes in the package must be public to be useable outside of the package.
- The name of a package (i.e.
myPackages.internet.ftp)
corresponds to a directory path (myPackages/internet/ftp/) containing a
set of classes.
Common packages
java.lang // Used by default
java.net // Internet reading, transfer, and communication
java.awt // Abstract windows toolkit, GUIs
java.io // Streams and files
java.util // Dates, random numbers, stacks, hash tables
Defining
- Specifies the package that a class is
within.
- Define a package at the top of the source
file:
package PackageName;
- If a package is not defined, the class
belong to the package
called "Default".
- By default, all classes in an application
can access all other
classes.
Importing
- Import a single class from a package:
import
PackageName.ClassName;
- Importing all classes from a package
(non-recursively):
import PackageName.*;
- If a package is imported, the package
name does not have to be
used when referring to classes within the package.
- java.lang is imported by default.
java.net // Internet reading, transfer, and communication
java.awt // Abstract windows toolkit, GUIs
java.io // Streams and files
java.util // Dates, random numbers, stacks, hash tables
- Specifies the package that a class is within.
- Define a package at the top of the source
file:
package PackageName; - If a package is not defined, the class belong to the package called "Default".
- By default, all classes in an application can access all other classes.
Importing
- Import a single class from a package:
import
PackageName.ClassName;
- Importing all classes from a package
(non-recursively):
import PackageName.*;
- If a package is imported, the package
name does not have to be
used when referring to classes within the package.
- java.lang is imported by default.
import
PackageName.ClassName;import PackageName.*;