📄 tij0037.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0036.html">Prev</a> | <a href="tij0038.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Building
a Java program
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are several other issues you must understand before seeing your first Java
program.
</FONT><a name="_Toc375545230"></a><a name="_Toc408018431"></a><P></DIV>
<A NAME="Heading77"></A><H3 ALIGN=LEFT>
Name
visibility
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
problem in any programming language is the control of names. If you use a name
in one module of the program, and another programmer uses the same name in
another module, how do you distinguish one name from another and prevent the
two names from “clashing”? In C this is a particular problem
because a program is often an unmanageable sea of names. C++ classes (on which
Java classes are based) nest functions within classes so they cannot clash with
function names nested within other classes. However, C++ still allowed global
data and global functions, so clashing was still possible. To solve this
problem, C++ introduced
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>namespaces</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
using additional keywords.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
was able to avoid all of this by taking a fresh approach. To produce an
unambiguous name for a library, the specifier used is not unlike an Internet
domain name. In fact, the Java creators want you to use your Internet domain
name in reverse since those are guaranteed to be unique. Since my domain name is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BruceEckel.com</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
my utility library of foibles would be named
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.bruceeckel.utility.foibles</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
After your reversed domain name, the dots are intended to represent
subdirectories.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
<A NAME="Index76"></A>Java
1.0 and Java 1.1<A NAME="Index77"></A>
the domain extension
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>edu</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>org</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>net</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
etc., was <A NAME="Index78"></A><A NAME="Index79"></A>capitalized
by convention, so the library would appear:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>COM.bruceeckel.utility.foibles</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Partway through the development of Java 1.2<A NAME="Index80"></A>,
however, it was discovered that this caused problems and so now the entire
package name is lowercase.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
mechanism in Java means that all of your files automatically live in their own
namespaces, and each class within a file automatically has a unique identifier.
(Class names within a file must be unique, of course.) So you do not need to
learn special language features to solve this problem – the language
takes care of it for you.
</FONT><a name="_Toc375545231"></a><a name="_Toc408018432"></a><P></DIV>
<A NAME="Heading78"></A><H3 ALIGN=LEFT>
Using
other components
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Whenever
you want to use a predefined class in your program, the compiler must know how
to locate it. Of course, the class might already exist in the same source code
file that it’s being called from. In that case, you simply use the class
– even if the class doesn’t get defined until later in the file.
Java eliminates the “forward referencing” problem so you
don’t need to think about it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
about a class that exists in some other file? You might think that the compiler
should be smart enough to simply go and find it, but there is a problem.
Imagine that you want to use a class of a particular name, but the definition
for that class exists in more than one file. Or worse, imagine that
you’re writing a program, and as you’re building it you add a new
class to your library that conflicts with the name of an existing class.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
solve this problem, you must eliminate all potential ambiguities. This is
accomplished by telling the Java compiler exactly what classes you want using
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">tells
the compiler to bring in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>package</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which is a library of classes. (In other languages, a library could consist of
functions and data as well as classes, but remember that all code in Java must
be written inside a class.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Most
of the time you’ll be using components from the standard Java libraries
that come with your compiler. With these, you don’t need to worry about
long, reversed domain names; you just say, for example:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
java.util.Vector;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">to
tell the compiler that you want to use Java’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class. However,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
contains a number of classes and you might want to use several of them without
declaring them all explicitly. This is easily accomplished by using ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>*</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’
to indicate a wildcard:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">import
java.util.*;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
is more common to import a collection of classes in this manner than to import
classes individually.
</FONT><a name="_Toc375545232"></a><a name="_Toc408018433"></a><P></DIV>
<A NAME="Heading79"></A><H3 ALIGN=LEFT>
The
static keyword
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Ordinarily,
when you create a class you are describing how objects of that class look and
how they will behave. You don’t actually get anything until you create an
object of that class with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and at that point data storage is created and methods become available.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">But
there are two situations in which this approach is not sufficient. One is if
you want to have only one piece of storage for a particular piece of data,
regardless of how many objects are created, or even if no objects are created.
The other is if you need a method that isn’t associated with any
particular object of this class. That is, you need a method that you can call
even if no objects are created. You can achieve both of these effects with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword. When you say something is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -