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

📄 tij0052.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
of creating an object. Consider a class called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	The
first time an object of type 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>or</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the first time a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">field
of class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is accessed, the Java interpreter must locate 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which it does by searching through the classpath.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	As
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is loaded (which creates a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object, which you&#8217;ll learn about later), all of its 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initializers are run. Thus, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">initialization
takes place only once, as the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is loaded for the first time.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	When
you create a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new
Dog(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the construction process for a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object first allocates enough storage for a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object on the heap.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	This
storage is wiped to zero, automatically setting all the primitives in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Dog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to their default values (zero for numbers and the equivalent for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Any
initializations that occur at the point of field definition are executed.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Constructors
are executed. As you shall see in Chapter 6, this might actually involve a fair
amount of activity, especially when inheritance is involved.
</FONT></OL><A NAME="Heading155"></A><H4 ALIGN=LEFT>
Explicit
static initialization
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
allows you to group other 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initializations inside a special &#8220;<A NAME="Index322"></A><A NAME="Index323"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
construction clause&#8221; (sometimes called a <A NAME="Index324"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>static
block
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">in
a class. It looks like this:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">class
Spoon {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
 static int i;
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
 static {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
   i = 47;
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
 }
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
 // . . .
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">So
it looks like a method, but it&#8217;s just 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 followed by a method body. This code, like the other 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initialization, is executed only once, the first time you make an object of
that class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>or</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you access a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member of that class (even if you never make an object of that class). For
example:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: ExplicitStatic.java</font>
<font color="#009900">// Explicit static initialization</font>
<font color="#009900">// with the "static" clause.</font>

<font color="#0000ff">class</font> Cup {
  Cup(<font color="#0000ff">int</font> marker) {
    System.out.println("Cup(" + marker + ")");
  }
  <font color="#0000ff">void</font> f(<font color="#0000ff">int</font> marker) {
    System.out.println("f(" + marker + ")");
  }
}

<font color="#0000ff">class</font> Cups {
  <font color="#0000ff">static</font> Cup c1;
  <font color="#0000ff">static</font> Cup c2;
  <font color="#0000ff">static</font> {
    c1 = <font color="#0000ff">new</font> Cup(1);
    c2 = <font color="#0000ff">new</font> Cup(2);
  }
  Cups() {
    System.out.println("Cups()");
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> ExplicitStatic {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    System.out.println("Inside main()");
    Cups.c1.f(99);  <font color="#009900">// (1)</font>
  }
  <font color="#0000ff">static</font> Cups x = <font color="#0000ff">new</font> Cups();  <font color="#009900">// (2)</font>
  <font color="#0000ff">static</font> Cups y = <font color="#0000ff">new</font> Cups();  <font color="#009900">// (2)  </TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">} ///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initializers for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cups</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will be run when either the access of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>c1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
occurs on the line marked (1), or if line (1) is commented out and the lines
marked (2) are uncommented. If both (1) and (2) are commented out, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initialization for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cups</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
never occurs.
</FONT><P></DIV>
<A NAME="Heading156"></A><H4 ALIGN=LEFT>
Non-static
instance initialization
<P><A NAME="Index325"></A><A NAME="Index326"></A><A NAME="Index327"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.1<A NAME="Index328"></A>
provides a similar syntax for initializing non-static variables for each
object. Here&#8217;s an example:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Mugs.java</font>
<font color="#009900">// Java 1.1 "Instance Initialization"</font>

<font color="#0000ff">class</font> Mug {
  Mug(<font color="#0000ff">int</font> marker) {
    System.out.println("Mug(" + marker + ")");
  }
  <font color="#0000ff">void</font> f(<font color="#0000ff">int</font> marker) {
    System.out.println("f(" + marker + ")");
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> Mugs {
  Mug c1;
  Mug c2;
  {
    c1 = <font color="#0000ff">new</font> Mug(1);
    c2 = <font color="#0000ff">new</font> Mug(2);
    System.out.println("c1 &amp; c2 initialized");
  }
  Mugs() {
    System.out.println("Mugs()");
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    System.out.println("Inside main()");
    Mugs x = <font color="#0000ff">new</font> Mugs();
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the instance initialization clause:
</FONT><P></DIV>

<font color="#990000"><PRE>  {
    c1 = <font color="#0000ff">new</font> Mug(1);
    c2 = <font color="#0000ff">new</font> Mug(2);
    System.out.println("c1 &amp; c2 initialized");
  } </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">looks
exactly like the static initialization clause except for the missing 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword. This syntax is necessary to support the initialization of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>anonymous
inner classes
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(see Chapter 7).
</FONT><a name="_Toc375545287"></a><a name="_Toc408018489"></a><a name="_Toc312373861"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn21" HREF="#fnB21">[21]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
In contrast, C++ has the 
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><I>constructor
initializer list
</I></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
that causes initialization to occur before entering the constructor body, and
is enforced for objects. See 
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><I>Thinking
in C++
</I></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">.</FONT><P></DIV>


<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0051.html">Prev</a> | <a href="tij0053.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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