It declares a variable str
that can hold a reference to a
String
object.
No object has been created.
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 exists,
an object is accessed by going through a reference.
Often, the reference is held in a reference variable such as str
.
For example,
the following first declares a reference variable,
then constructs an object and puts the reference to it in str
:
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" ;