The Essence of OOP using Java: Static Members…

2008-02-23 09:45:59来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Listing 3

Display date information

Proceeding down through the code in the main method, the code in Listing 4 causes the current contents of the Date object referred to by the contents of the class variable named v1 to be displayed on the computer screen.

    System.out.println(MyClass01.v1);



Listing 4

No object required

For the moment, concentrate on the boldface text in the statement in Listing 4.

IMPORTANT: Because the variable named v1 is a class variable, it's value is accessed by joining the name of the class to the name of the variable with a period.

What was the output?

I will also discuss the remaining portion of statements of this sort later. For now, just be aware that it caused the output shown in Figure 1 to be displayed on my computer screen when I ran the program.

Mon Sep 17 09:52:27 CDT 2001



Figure 1

Displays date and time

Obviously, the date and time displayed will depend on when you run the program.

Pay particular attention to the seconds portion of the time. I will refer back to this later.

A five-second delay

The code in Listing 5 (still in the main method) causes the main thread of the program to go to sleep for five seconds. Don't worry about it if you don't understand this code. The only reason that I included it in the program was to force a five-second delay in the execution of the program.

  try{

    Thread.currentThread().sleep(5000);

  }catch(InterruptedException e){}



Listing 5

Instantiate a new object

Having caused the program to sleep for five seconds, the code in Listing 6 instantiates a new object of the class named MyClass01. The code stores the new object's reference in the reference variable named ref1.

    MyClass01 ref1 = new MyClass01();



Listing 6

A new Date object also

Recall from Listing 1 above that the class declares an instance variable named v2 of the type Date.

When the new object is instantiated by the code in Listing 6, a new Date object is also instantiated. A reference to that object is stored in the instance variable named v2.

(In other words, the new object of the class MyClass01 owns a reference to a new object of the class 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),

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Tomcat配置SQLServer连接池

下一篇:struts spring hibernate架构中数据对象(PO,POJO,FormBean)的