手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

The Essence of OOP using Java: Static Members

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Date. That reference is stored in an instance variable named v2 in the new MyClass01 object.)Display the new Date object

The code in Listing 7 causes a textual representation of the new Date object referred to by the reference variable named v2 belonging to the object referred to by the reference variable named ref1, to be displayed on the standard output device.

    System.out.println(ref1.v2);



Listing 7

Five seconds later

This code caused the date and time shown in Figure 2 to appear on the computer screen when I ran the program:

Mon Sep 17 09:52:32 CDT 2001



Figure 2

If you note the time in the above output, you will see that it is five seconds later than the time reflected in the Date object referred to by the class variable named v1. That time was displayed by the code in Listing 4 earlier.

So, what does this mean?

It means that the Date object referred to by the static reference variable named v1 was created five seconds earlier than the Date object referred to by the instance variable named v2.

When is a class variable created?

I can't tell you precisely when a class variable comes into existence. All I can say is that the virtual machine brings it into existence as soon as it is needed.

My guess is that it comes into existence at the first mention (in the program) of the class to which it belongs.

When is an instance variable created?

An instance variable doesn't come into existence until the object to which it belongs is created (an instance variable cannot exist until the object to which it belongs exists).

If the instance variable is initialized with a reference to a new object (such as a new Date object in this sample program), that new object comes into existence when the object to which it belongs comes into existence.

A five-second delay

In this program, I purposely inserted a five-second delay between the first mention of the class named MyClass01 in Listing 4, and the instantiation of the object of the class named MyClass01 in Listing 6.

As a result, the Date object referred to by the instance variable named v2 was created about five seconds later than the Date object referred to by the class variable named v1.

This is reflected in the date and time values displayed and discussed earlier.

Accessing class variable via an object

While it is possible to access a class variable using the name of the class joined to the name of the variable, it is also possible to access a class variable using a reference to any object instantiated from the class.

(As mentioned earlier, if two or more objects are instantiated from the same class, they share the same class variable.)
The boldface code in Listing 8 uses the reference variable named ref1 to access the class variable named v1, and to cause the contents of the Date object referred to by the class variable to be displayed.
    System.out.println(ref1.v1);



Listing 8

The output

This caused the date and time shown in Figure 3 to be displayed on my computer screen.

Mon Sep 17 09:52:27 CDT 2001



Figure 3

Same date and time

As you have probably already surmised, this is the same date and time shown earlier in Figure 1. This is because the code in Listing 8 refers to the same class variable as the code in Listing 4. Nothing has caused the contents of that class variable to change, so both Figure 1 and Figure 3 display the contents of the same Date object.

(Only one class variable exists and it doesn't matter how you access it. Either way, you gain access to the same Date object whose reference is stored in the class variable. Thus, the same date and time is shown in both cases.)
Another new object

If you examine the code in Listing 13 near the end of the program, you will see that an additional five-second delay is introduced at this point in the program.

Following that delay, the code in Listing 9 instantiates another new object of the class named MyClass01, and stores the object's reference in a new reference variable named ref2.

(The object referred to by ref1 is a different object than the object referred to by

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!