Java Reference
In-Depth Information
java.lang.System.out.println(x);
Because
println
is followed by an opening parenthesis, it must be a method
name. Therefore,
out
must be either an object or a class with a static
println
method. (Of course, we know that
out
is an object reference of type
PrintStream
.) Again, it is not at all clear, without context, whether
System
is another object, with a public variable
out
, or a class with a
static
variable.
Judging from the number of pages that the Java language specification [
3
]
devotes to this issue, even the compiler has trouble interpreting these
dot-separated sequences of strings.
To avoid problems, it is helpful to adopt a strict coding style. If class names
always start with an uppercase letter, and variable, method, and package names
always start with a lowercase letter, then confusion can be avoided.
H
OW
T
O
8.1: Programming with Packages
This How To explains in detail how to place your programs into packages. For
example, your instructor may ask you to place each homework assignment into a
separate package. That way, you can have classes with the same name but
different implementations in separate packages (such as
homework1.Bank
and
homework2.Bank
).
Step 1 Come up with a package name.
Your instructor may give you a package name to use, such as
homework1
. Or,
perhaps you want to use a package name that is unique to you. Start with your
e-mail address, written backwards. For example,
walters@cs.sjsu.edu
becomes
edu.sjsu.cs.walters
. Then add a sub-package that describes
your project or homework, such as
edu.sjsu.cs. walters.
homework1
.
Step 2 Pick a base directory.
The base directory is the directory that contains the directories for your various
packages, for example,
/home/walters
or
c:\cs1
368