您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 29

Packages and imports

In Java, it is usual for classes to be grouped into a package, i.e., a set of related classes. A `packaged' class must be identified by its package name and class name, or the 'import' instruction must be used to tell the compiler to look for classes in a particular package.

You may have noticed at the start of many of the example programs a line like this:

import java.applet.Applet;

This is an indication that the program will make use of the class Applet which is in the package java.applet.

The Java developers have designed the compiler to automatically import the standard Java package java.lang. This defines important classes such as String. You will need to refer to a Java reference, such as Deitel & Deitel or the on-line documentation to identify what to import to use those classes that are not 'built-in' through automatic importing.

You will also have seen the line

import java.awt.*;

This means 'search every class in the package java.awt'. When the compiler comes across a class it does not recognise in the current program, it will see if it exists inside the package java.awt.

Because an applet imports java.applet.Applet, then we can write the following statement (secure in the knowledge the Java compiler knows where to find the Applet class being extended):

class MyApplet extends Applet

If we did not use the import line, the program would still compile as long as I wrote

class MyApplet extends java.applet.Applet

If you are creating a large, sophisticated program, you may well want to group your classes up into packages of related classes.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg