go to previous page   go to home page   go to next page


It declares a variable str that can hold a reference to a String object. No object has been created.

Review of Object References

Object and Reference

An object exists only after it has been constructed. Objects are constructed only as a program is running when an object constructor is invoked.

Once it has been constructed, an object is accessed by following a reference to the object. Often, the reference is held in a reference variable such as str. In the the following example, a reference variable is declared, an object is constructed, and then the reference to the object is put in the reference variable:

String str;             // declare a reference variable
str = "Hello World" ;   // construct the object and 
                        // save its reference

Of course, it is OK to declare an object reference variable and not place a reference in it (it might only sometimes be needed.) And it is OK to use the same reference variable for different objects at different times:

String str; 
str = "Hello World" ;

. . . .

str = "Good-by" ;                     


(Thought Question: ) What could you do if your program needed to keep track of 1000 different strings?

go to previous page   go to home page   go to next page