package
statement with that name at the top of every source file that
contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.
The package statement (for example, package graphics;
) must be the first line in the source file. There can be
only one package statement in each source file, and it applies to all types in the file.
public
,
and it must have the same name as the source file. For example, you can define
public class Circle
in the file Circle.java
, define public interface Draggable
in the file Draggable.java
, define public enum Day
in the file Day.java
, and so forth.
You can include non-public types in the same file as a public type (this is strongly discouraged, unless the non-public types are small and closely related to the public type), but only the public type will be accessible from outside of the package. All the top-level, non-public types will be package private.
graphics
,
you would need six source files, like this:
//in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . }
package
statement, your type
ends up in an unnamed package. Generally speaking, an unnamed package
is only for small or temporary applications or when you are just
beginning the development process.
Otherwise, classes and interfaces belong
in named packages.