The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
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 ref2. Each object has its own instance variable named v2, and in this case, each instance variable is initialized to instantiate and refer to a new Date object when the new MyClass01 object is instantiated.)
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.)标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
