<< Chapter < Page Chapter >> Page >
Listing 8 . Accessing class variable via an object.
System.out.println(ref1.v1);

The output

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

Figure 3 . Same date and time as before.
Mon Sep 17 09:52:27 CDT 2001

Same date and time as before

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 shownin 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 introducedat 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.)

Listing 9 . Another new object .
MyClass01 ref2 = new MyClass01();

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.

Listing 10 . Display the date and time .
System.out.println(ref2.v2);

This caused the output shown in Figure 4 to be displayed on my computer screen when I ran the program.

(Once again, you will get different results if you compile and run the program because the date and time shown is the dateand time that you run the program.)

Figure 4 . A new date and time.
Mon Sep 17 09:52:37 CDT 2001

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 theinstantiation 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 ofinstance 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 itbelongs ceases to exist.

Eligible for garbage collection

If the instance variables are reference variables holding references to other objects, as is the case here, and if there are no other referencevariables holding references to those same objects, the secondary objects cease to exist when the primary objects cease to exist.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask