Java Reference
In-Depth Information
AJava class definition must fit into one file; it cannot be split among multiple files.
Only one
public
class allowed per file (we discuss access modifiers in Section 5.3.3).
The file name must match exactly, including usage of upper and lower case characters,
the name of the
public
class definition in the file plus the extension
“.java
„
.
It is also recommended that each class definition appear in its own file except for
small helper classes used only by the primary class. The compilation of a Java
file results in separate
“.class
„
files for each class definition in the file.
5.3 Packages
A package is the Java version of a library. It allows for the grouping of
“.class
„
files that share a common purpose. The package organization follows that of a
hierarchical file directory system. To create a package you place a group of Java
source files into the same directory and in each source file include a
package
directive with the name of the directory at the top of the file.
Forexample, we put the files
TestA.java
and
TestB.java
into the direc-
tory
mypack
,which is a subdirectory of
myApps
.OnaWindows platform the
file paths might look like
C:
\
myApps
\
mypack
\
TestA.java
and
C:
\
myApps
\
mypack
\
TestB.java
At the top of each file we insert the statement
package mypack;
as shown in the following code:
package mypack;
public class
TestA
{
public int a;
public TestA (int arg1)
{
a
=
arg1;
package mypack;
public class
TestB
{
public double x;
public TestB (double y)
{
x
=
y;
}
}
}
}
A program that needs to use these classes must include, either explicitly or implic-
itly (we discuss the
import
directive below), the package name wherever it gives
Search WWH ::
Custom Search