⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0071.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<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="tij0070.html">Prev</a> | <a href="tij0072.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
The
final keyword
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index503"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword has slightly different meanings depending on the context, but in
general it says &#8220;This cannot be changed.&#8221; You might want to prevent
changes for two reasons: design or efficiency. Because these two reasons are
quite different, it&#8217;s possible to misuse the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following sections discuss the three places where 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be used: for data, methods and for a class.
</FONT><a name="_Toc375545318"></a><a name="_Toc408018521"></a><P></DIV>
<A NAME="Heading197"></A><H3 ALIGN=LEFT>
Final
data
<P><A NAME="Index504"></A><A NAME="Index505"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Many
programming languages have a way to tell the compiler that a piece of data is
&#8220;constant.&#8221; A constant is useful for two reasons: 
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	It
can be a <A NAME="Index506"></A><A NAME="Index507"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>compile-time
constant
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that won&#8217;t ever change.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	It
can be a value initialized at <A NAME="Index508"></A>run-time
that you don&#8217;t want changed.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the case of a compile-time constant the compiler is allowed to
&#8220;fold&#8221; the <A NAME="Index509"></A><A NAME="Index510"></A>constant
value into any calculations in which it&#8217;s used; that is, the calculation
can be performed at compile time, eliminating some run-time overhead. In Java,
these sorts of constants must be <A NAME="Index511"></A>primitives
and are expressed using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword. A value must be given at the time of definition of such a constant.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
field that is both <A NAME="Index512"></A><A NAME="Index513"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
has only one piece of storage that cannot be changed.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
using <A NAME="Index514"></A><A NAME="Index515"></A><A NAME="Index516"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with object handles rather than primitives the meaning gets a bit confusing.
With a primitive, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
makes the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>value</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a constant, but with an object handle, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
makes the handle a constant. The handle must be initialized to an object at the
point of declaration, and the handle can never be changed to point to another
object. However, the object can be modified; Java does not provide a way to
make any arbitrary object a constant. (You can, however, write your class so
that objects have the effect of being constant.) This restriction includes
arrays, which are also objects.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
an example that demonstrates 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
fields:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: FinalData.java</font>
<font color="#009900">// The effect of final on fields</font>

<font color="#0000ff">class</font> Value {
  <font color="#0000ff">int</font> i = 1;
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> FinalData {
  <font color="#009900">// Can be compile-time constants</font>
  <font color="#0000ff">final</font> <font color="#0000ff">int</font> i1 = 9;
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> I2 = 99;
  <font color="#009900">// Typical public constant:</font>
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> I3 = 39;
  <font color="#009900">// Cannot be compile-time constants:</font>
  <font color="#0000ff">final</font> <font color="#0000ff">int</font> i4 = (<font color="#0000ff">int</font>)(Math.random()*20);
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> <font color="#0000ff">int</font> i5 = (<font color="#0000ff">int</font>)(Math.random()*20);
  
  Value v1 = <font color="#0000ff">new</font> Value();
  <font color="#0000ff">final</font> Value v2 = <font color="#0000ff">new</font> Value();
  <font color="#0000ff">static</font> <font color="#0000ff">final</font> Value v3 = <font color="#0000ff">new</font> Value();
  <font color="#009900">//! final Value v4; // Pre-Java 1.1 Error: </font>
                      <font color="#009900">// no initializer</font>
  <font color="#009900">// Arrays:</font>
  <font color="#0000ff">final</font> <font color="#0000ff">int</font>[] a = { 1, 2, 3, 4, 5, 6 };

  <font color="#0000ff">public</font> <font color="#0000ff">void</font> print(String id) {
    System.out.println(
      id + ": " + "i4 = " + i4 + 
      ", i5 = " + i5);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    FinalData fd1 = <font color="#0000ff">new</font> FinalData();
    <font color="#009900">//! fd1.i1++; // Error: can't change value</font>
    fd1.v2.i++; <font color="#009900">// Object isn't constant!</font>
    fd1.v1 = <font color="#0000ff">new</font> Value(); <font color="#009900">// OK -- not final</font>
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; fd1.a.length; i++)
      fd1.a[i]++; <font color="#009900">// Object isn't constant!</font>
    <font color="#009900">//! fd1.v2 = new Value(); // Error: Can't </font>
    <font color="#009900">//! fd1.v3 = new Value(); // change handle</font>
    <font color="#009900">//! fd1.a = new int[3];</font>

    fd1.print("fd1");
    System.out.println("Creating <font color="#0000ff">new</font> FinalData");
    FinalData fd2 = <font color="#0000ff">new</font> FinalData();
    fd1.print("fd1");
    fd2.print("fd2");
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>I2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
primitives with compile-time values, they can both be used as compile-time
constants and are not different in any important way. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>I3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the more typical way you&#8217;ll see such constants defined: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so they&#8217;re usable outside the package, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to emphasize that there&#8217;s only one, and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to say that it&#8217;s a constant. Note that <A NAME="Index517"></A><A NAME="Index518"></A><A NAME="Index519"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final
static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
primitives with constant initial values (that is, compile-time constants) are
named with all capitals by convention. Also note that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i5</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
cannot be known at compile time, so it is not capitalized.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Just
because something is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
doesn&#8217;t mean that its value is known at compile-time. This is
demonstrated by initializing 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i4</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i5</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
at run-time using randomly generated numbers. This portion of the example also
shows the difference between making a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
value 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This difference shows up only when the values are initialized at run-time,
since the compile-time values are treated the same by the compiler. (And

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -