<< Chapter < Page Chapter >> Page >

Technically, the objects may not actually cease to exist. Technically they become eligible for garbage collection, which means that the memory thatthey occupy becomes eligible for reuse. However, as a practical matter, they cease to exist insofar as the program is concerned because they are no longeraccessible.

A five-second difference in the time of creation

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 timeencapsulated 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 onecopy.

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.

Listing 11 . Display date information.
System.out.println(ref2.v1); }//end main

The output produced by the code in Listing 11 is shown in Figure 5 .

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

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 thename 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 samephysical class variable.

Since nothing was done to cause the contents of the class variable to change after it came into existence and was initialized, Figure 1 , Figure 3 , and Figure 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.

Listing 12 . Revisiting System.out.println.
System.out.println(ref1.v1);

Java programmer wanted

I sometimes tell my students that if I were out in industry interviewing prospective Java programmers, my first question would be to ask the prospectiveemployee to tell me everything that she knows about the statement in Listing 12 .

Covers a lot of Java OOP technology

This is not because there is a great demand for the use of this statement in real-world problems. (In fact, in a GUI-driven software product world, there is probably very little demand for the use of this statement.) Rather, it is because a lot of Java object-oriented technology is embodied in this singlestatement.

In that scenario, I would expect to receive a verbal dissertation of fifteen to twenty minutes in length to cover all the important points.

The short version

Let me give you the short version. There is a class named System . The System class declares three static (class) variables having the following types, names, and modifiers:

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