|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
To create a package, you put a type (class, interface, enum or annotation) in it. To do this, you put apackagestatement at the top of the source file in which the type is defined. For example, the following code appears in the source fileCircle.javaand puts theCircleclass in thegraphicspackage:Thepackage graphics; public class Circle extends Graphic implements Draggable { . . . }Circleclass is a public member of thegraphicspackage.You must include a
packagestatement at the top of every source file that defines a class or an interface that is to be a member of thegraphicspackage. So you would also include the statement inRectangle.javaand so on:The scope of thepackage graphics; public class Rectangle extends Graphic implements Draggable { . . . }packagestatement is the entire source file, so all classes, interfaces, enums and annotations defined inCircle.javaandRectangle.javaare also members of thegraphicspackage. If you put multiple classes in a single source file, only one may be public, and it must share the name of the source file's base name. Only public package members are accessible from outside the package.If you do not use a
Note: Some compilers might allow more than one public class per.javafile. However, we recommend that you use the convention one public class per file, because it makes public classes easier to find and works for all compilers.packagestatement, your type ends up in the default package, which is a package that has no name. Generally speaking, the default package is only for small or temporary applications or when you are just beginning development. Otherwise, classes, enums and annotations belong in named packages.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.