📄 tij0052.html
字号:
scattered throughout in between method definitions, the variables are
initialized before any methods can be called – even the constructor. For
example:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: OrderOfInitialization.java</font>
<font color="#009900">// Demonstrates initialization order.</font>
<font color="#009900">// When the constructor is called, to create a</font>
<font color="#009900">// Tag object, you'll see a message:</font>
<font color="#0000ff">class</font> Tag {
Tag(<font color="#0000ff">int</font> marker) {
System.out.println("Tag(" + marker + ")");
}
}
<font color="#0000ff">class</font> Card {
Tag t1 = <font color="#0000ff">new</font> Tag(1); <font color="#009900">// Before constructor</font>
Card() {
<font color="#009900">// Indicate we're in the constructor:</font>
System.out.println("Card()");
t3 = <font color="#0000ff">new</font> Tag(33); <font color="#009900">// Re-initialize t3</font>
}
Tag t2 = <font color="#0000ff">new</font> Tag(2); <font color="#009900">// After constructor</font>
<font color="#0000ff">void</font> f() {
System.out.println("f()");
}
Tag t3 = <font color="#0000ff">new</font> Tag(3); <font color="#009900">// At end</font>
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> OrderOfInitialization {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Card t = <font color="#0000ff">new</font> Card();
t.f(); <font color="#009900">// Shows that construction is done</font>
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Card</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the definitions of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Tag</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are intentionally scattered about to prove that they’ll all get
initialized before the constructor is entered or anything else can happen. In
addition,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>t3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is re-initialized inside the constructor. The output is:
</FONT><P></DIV>
<font color="#990000"><PRE>Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
f()</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Thus,
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>t3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle gets initialized twice, once before and once during the constructor
call. (The first object is dropped, so it can be garbage-collected later.) This
might not seem efficient at first, but it guarantees proper initialization
– what would happen if an overloaded constructor were defined that did
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initialize
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>t3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and there wasn’t a “default” initialization for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>t3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in its definition?
</FONT><P></DIV>
<A NAME="Heading154"></A><H4 ALIGN=LEFT>
Static
data initialization
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
the <A NAME="Index319"></A><A NAME="Index320"></A>data
is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the same thing happens; if it’s a primitive and you don’t
initialize it, it gets the standard primitive initial values. If it’s a
handle to an object, it’s null unless you create a new object and attach
your handle to it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you want to place initialization at the point of definition, it looks the same
as for non-statics. But since there’s only a single piece of storage for a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
regardless of how many objects are created the question of when that storage
gets initialized arises. An example makes this question clear:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: StaticInitialization.java</font>
<font color="#009900">// Specifying initial values in a</font>
<font color="#009900">// class definition.</font>
<font color="#0000ff">class</font> Bowl {
Bowl(<font color="#0000ff">int</font> marker) {
System.out.println("Bowl(" + marker + ")");
}
<font color="#0000ff">void</font> f(<font color="#0000ff">int</font> marker) {
System.out.println("f(" + marker + ")");
}
}
<font color="#0000ff">class</font> Table {
<font color="#0000ff">static</font> Bowl b1 = <font color="#0000ff">new</font> Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
<font color="#0000ff">void</font> f2(<font color="#0000ff">int</font> marker) {
System.out.println("f2(" + marker + ")");
}
<font color="#0000ff">static</font> Bowl b2 = <font color="#0000ff">new</font> Bowl(2);
}
<font color="#0000ff">class</font> Cupboard {
Bowl b3 = <font color="#0000ff">new</font> Bowl(3);
<font color="#0000ff">static</font> Bowl b4 = <font color="#0000ff">new</font> Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
<font color="#0000ff">void</font> f3(<font color="#0000ff">int</font> marker) {
System.out.println("f3(" + marker + ")");
}
<font color="#0000ff">static</font> Bowl b5 = <font color="#0000ff">new</font> Bowl(5);
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> StaticInitialization {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
System.out.println(
"Creating <font color="#0000ff">new</font> Cupboard() in main");
<font color="#0000ff">new</font> Cupboard();
System.out.println(
"Creating <font color="#0000ff">new</font> Cupboard() in main");
<font color="#0000ff">new</font> Cupboard();
t2.f2(1);
t3.f3(1);
}
<font color="#0000ff">static</font> Table t2 = <font color="#0000ff">new</font> Table();
<font color="#0000ff">static</font> Cupboard t3 = <font color="#0000ff">new</font> Cupboard();
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Bowl</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
allows you to view the creation of a class, and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Table</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cupboard</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
create
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Bowl</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
scattered through their class definitions. Note that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cupboard</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
creates a non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Bowl
b3
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
prior to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
definitions. The output shows what happens:
</FONT><P></DIV>
<font color="#990000"><PRE>Bowl(1)
Bowl(2)
Table()
f(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f(2)
Creating <font color="#0000ff">new</font> Cupboard() in main
Bowl(3)
Cupboard()
f(2)
Creating <font color="#0000ff">new</font> Cupboard() in main
Bowl(3)
Cupboard()
f(2)
f2(1)
f3(1)</PRE></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">
initialization occurs only if it’s necessary. If you don’t create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Table
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">object
and you never refer to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Table.b1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">or
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Table.b2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
Bowl b1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>b2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">will
never be created. However, they are created only when the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>first
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Table
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">object
is created (or the first
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
access occurs). After that, 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 is not re-initialized.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
order of initialization is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
first, if they haven’t already been initialized by a previous object
creation, and then the non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. You can see the evidence of this in the output.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
helpful to summarize the <A NAME="Index321"></A>process
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -