Java Reference
In-Depth Information
Output
x: 20
Expected: 20
y: 35
Expected: 35
48
49
For this program, we needed to carry out another step: We needed to import the
Rectangle
class from a package. A package is a collection of classes with a related
purpose. 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.
To use the
Rectangle
class from the
java.awt
package, simply place the
following line at the top of your program:
import java.awt.Rectangle;
Why don't you have to import the
System
and
String
classes? Because the
System
and
String
classes are in the
java.lang
package, and all classes from
this package are automatically imported, so you never need to import them yourself.
S
YNTAX
2.4 Importing a Class from a Package
import packageName.ClassName;
Example:
import java.awt.Rectangle;
Purpose
To import a class from a package for use in a program