|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 4
Listing 1. A.java
// A.java
package testpkg;
public class A
{
int x = 1;
public int y = 2;
protected int z = 3;
int returnx ()
{
return x;
}
public int returny ()
{
return y;
}
protected int returnz ()
{
return z;
}
public interface StartStop
{
void start ();
void stop ();
}
}
class B
{
public static void hello ()
{
System.out.println ("hello");
}
}
Listing 1 introduces the source code to your first named package. The package testpkg; directive names that package testpkg. Within testpkg are classes A and B. Within A are three field declarations, three method declarations, and an inner interface declaration. Within B is a single method declaration. The entire source code stores in A.java because A is a public class. Our task: Turn this source code into a package that consists of two classes and an inner interface (or
a directory that contains three classfiles). The following Windows-specific steps accomplish that task:
c: drive's root directory (the main directory—represented by an initial backslash (\) character). To do that, type the c: command followed by the cd \ command. (If you use a different drive, replace c: with your chosen drive. Also, do not forget to press the Enter key after typing a command.)
testpkg directory by typing md testpkg. Note: When following this article's steps, do not type periods after the commands.
testpkg the current directory by typing cd testpkg.
A.java file in testpkg.
A.java by typing javac A.java. You should see classfiles A$StartStop.class, A.class, and B.class appear in the testpkg directory.
Figure 1 illustrates Steps 3 through 5.

Figure 1. The testpkg directory with its three classfiles equates to a testpkg package with classes A and B, and A's inner interface StartStop
Congratulations! You have just created your first package. Think of this package as containing two classes (A and B) and A's single inner interface (StartStop). You can also think of this package as a directory containing three classfiles: A$StartStop.class, A.class, and B.class.
| Note |
|---|
To minimize package name conflicts (especially among commercial packages), Sun has established a convention in which a company's
Internet domain name reverses and prefixes a package name. For example, a company with x.com as its Internet domain name and a.b as a package name (a) followed by a subpackage name (b) prefixes com.x to a.b, resulting in com.x.a.b. My article does not follow this convention because the testpkg package is a throw-away designed for teaching purposes only.
|
Once you have a package, you will want to import classes and/or interfaces—actually, class and/or interface names—from that package to your program, so it can use those classes and/or interfaces. One way to accomplish that task is to supply the fully qualified package name (the package name and all subpackage names) in each place where the reference type name (the class or interface name) appears, as Listing 2 demonstrates: