Just as the program is about to close,
how many objects are there 4
and how many object references are there? 4
Has any garbage been created? Not yet.
It the example program, a String object was created, and a reference to it was kept in a reference variable. This worked, but was a bit awkward. Here is another modification to the example program:
import java.awt.*;
class PointEg3
{
public static void main ( String arg[] )
{
Point a = new Point(); // declarations and construction combined
Point b = new Point( 12, 45 );
Point c = new Point( b );
System.out.println( a.toString() ); // create a temporary String based on "a"
}
}
|
This program creates three points with the same values as before, but now the declaration and construction of each point is combined.
The last statement has the same effect as the last two statements of the previous program:
a
refers to an object with data (0,0).System.out.println( reference to a String );
Since the String reference was not saved in a reference variable, there is now no way to find it. It is garbage. That is OK. It was only needed for one purpose, and that purpose is completed.
Using objects in this manner is very common.