<< Chapter < Page Chapter >> Page >

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.

Listing 7 . Display the new Date object .
System.out.println(ref1.v2);

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:

Figure 2 . Five seconds later.
Mon Sep 17 09:52:32 CDT 2001

The date and time shown in Figure 2 is five seconds later than the time reflected in the Date object referred to by the class variable named v1 (see Figure 1 ) . 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 isneeded.

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 intoexistence.

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 above.

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 classvariable 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 code in parentheses 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.

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