MyClass01 ref2 = new MyClass01(); Listing 9
Display the date and time
Then, the code in Listing 10 causes the contents of the Date object referred to by the instance variable named v2 in the second object of the class named MyClass01 to be displayed.
System.out.println(ref2.v2); Listing 10
This caused the output shown in Figure 4 to be displayed on my computer screen when I ran the program (again, you will get different results if you compile and run the program because the date and time shown is the date and time that you run the program).
Mon Sep 17 09:52:37 CDT 2001 Figure 4
Five seconds later
As you have probably figured out by now, the time encapsulated in this Date object is five seconds later than the time encapsulated in the Date object displayed in Figure 2. This is because the program was put to sleep for five seconds between the instantiation of the two objects referred to by ref1 and ref2.
Every object has one
Every object instantiated from a given class has its own copy of each instance variable declared in the class definition. There is no sharing of instance variables among objects.
Each instance variable comes into existence when the object to which it belongs comes into existence, and ceases to exist when the object to which it belongs ceases to exist.
(If the instance variables are reference variables holding references to other objects, as is the case here, and if there are no other reference variables holding references to those same objects, the secondary objects cease to exist when the primary objects cease to exist. Technically, the objects may not cease to exist. Technically they become eligible for garbage collection, which means that the memory that they occupy becomes eligible for reuse. However, as a practical matter, they cease to exist insofar as the program is concerned. They are no longer accessible.)Since the two objects referred to by ref1 and ref2 came into existence with a five-second delay, the Date objects belonging to those two object reflect a five-second difference in the time encapsulated in the objects.
Only one copy of class variable exists
Also remember that if a variable is a class variable, only one copy of the variable exists, and all objects instantiated from the class share that one copy.
This is illustrated by the code in Listing 11, which uses the reference to the second object instantiated from the class named MyClass01, to cause the contents of the class variable named v1 to be displayed.
System.out.println(ref2.v1); }//end main Listing 11
The output produced by the code in Listing 11 is shown in Figure 5.
Mon Sep 17 09:52:27 CDT 2001 Figure 5
Same output as before
As you can see, this is the same as the output shown in Figure 1 and Figure 3 earlier.
Accessing the same physical class variable
Since only one class variable named v1 exists, and all objects instantiated from the class named MyClass01 share that single copy, it doesn't matter whether you access the class variable using the name of the class, or access it using a reference to either of the objects instantiated from the class. In all three cases, you are accessing the same physical class variable.
Since nothing was done to cause the contents of the class variable to change after it came into existence and was initialized, Figures 1, 3, and 5 are simply three different displays of the date and time encapsulated in the same Date object whose reference is stored in the class variable.
Let's revisit System.out.println...
Now, I want to revisit the statement originally shown in Listing 8 and repeated in Listing 12 for viewing convenience.
System.out.println(ref1.v1); Listing 12
Java programmer wanted
I often tell my students that if I were out in industry, interviewing prospective Java programmers, my first question would be to ask the prospective employee to tell me everything that she knows about the statement in Listing 12.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



