📄 tij0037.html
字号:
it means that data or method is not tied to any particular object instance of
that class. So even if you’ve never created an object of that class you
can call 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 access a piece of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data. With ordinary, non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data and methods you must create an object and use that object to access the
data or method, since non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data and methods must know the particular object they are working with. Of
course, since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods don’t need any objects to be created before they are used, they
cannot
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>directly
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">access
non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members or methods by simply calling those other members without referring to a
named object (since non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members and methods must be tied to a particular object).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Some
object-oriented languages use the terms
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>class
data
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>class
methods
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
meaning that the data and methods exist only for the class as a whole, and not
for any particular objects of the class. Sometimes the Java literature uses
these terms too.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
make a data member or method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you simply place the keyword before the definition. For example, this produces a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data member and initializes it:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">class</font> StaticTest {
<font color="#0000ff">static</font> <font color="#0000ff">int</font> i = 47;
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Now
even if you make two
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StaticTest</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, there will still be only one piece of storage for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StaticTest.i.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
Both objects will share the same
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i.
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Consider:</FONT><P></DIV>
<font color="#990000"><PRE>StaticTest st1 = <font color="#0000ff">new</font> StaticTest();
StaticTest st2 = <font color="#0000ff">new</font> StaticTest(); </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>st1.i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>st2.i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
have the same value of 47 since they refer to the same piece of memory.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are two ways to refer to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
variable. As indicated above, you can name it via an object, by saying, for
example,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>st2.i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You can also refer to it directly through its class name, something you cannot
do with a non-static member. (This is the preferred way to refer to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
variable since it emphasizes that variable’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
nature.)
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">StaticTest.i++;</FONT></TT><P></DIV><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>++</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
operator increments the variable. At this point, both
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>st1.i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>st2.i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will have the value 48.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Similar
logic applies to static methods. You can refer to a static method either
through an object as you can with any method, or with the special additional
syntax
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>classname.method( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You define a static method in a similar way:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#0000ff">class</font> StaticFun {
<font color="#0000ff">static</font> <font color="#0000ff">void</font> incr() { StaticTest.i++; }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StaticFun</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>incr( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
increments the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You can call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>incr( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the typical way, through an object:
</FONT><P></DIV>
<font color="#990000"><PRE>StaticFun sf = <font color="#0000ff">new</font> StaticFun();
sf.incr();</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Or,
because
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>incr( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
a static method, you can call it directly through its class:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">StaticFun.incr();</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">While
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
when applied to a data member, definitely changes the way the data is created
(one for each class vs. 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">one
for each object), when applied to a method it’s not so dramatic. An
important use of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for methods is to allow you to call that method without creating an object.
This is essential, as we will see, in defining the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that is the entry point for running an application.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Like
any method, 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 can create or use named objects of its type, so 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 is often used as a “shepherd” for a flock of instances of
its own type.
</FONT><a name="_Toc375545233"></a><a name="_Toc408018434"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0036.html">Prev</a> | <a href="tij0038.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -