Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Packages
Creating and Using Packages
Creating a Package
Naming a Package
Using Package Members
Managing Source and Class Files
Summary of Creating and Using Packages
Questions and Exercises
Trail: Learning the Java Language
Lesson: Packages
Home Page >Learning the Java Language >Packages
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Creating and Using Packages

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.


Definition: Apackage is a grouping of related types providing access protection and name space management. Note thattypes refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, sotypes are often referred to in this lesson simply asclasses and interfaces.

The types that are part of the Java platform are members of various packages that bundle classes by function: fundamental classes are injava.lang, classes for reading and writing (input and output) are injava.io, and so on. You can put your types in packages too.

Suppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, and points. You also write an interface,Draggable, that classes implement if they can be dragged with the mouse.

//in the Draggable.java filepublic interface Draggable {    ...}//in the Graphic.java filepublic abstract class Graphic {    ...}//in the Circle.java filepublic class Circle extends Graphic    implements Draggable {    . . .}//in the Rectangle.java filepublic class Rectangle extends Graphic    implements Draggable {    . . .}//in the Point.java filepublic class Point extends Graphic    implements Draggable {    . . .}//in the Line.java filepublic class Line extends Graphic    implements Draggable {    . . .}

You should bundle these classes and the interface in a package for several reasons, including the following:

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Packages
Next page: Creating a Package

[8]ページ先頭

©2009-2025 Movatter.jp