No. Just because there is a name for an object does not mean an object exists.
A variable that can refer to an object does not always have an object to refer to.
For example, in our program
the variable str1
refers to an object only
after the new
operator has created one.
class StringTester
{
public static void main ( String[] args )
{
String str1; // str1 is a variable that refers to an object,
// but the object does not exist yet.
int len; // len is a primitive variable of type int
str1 = new String("Random Jottings"); // create an object of type String
len = str1.length(); // invoke the object's method length()
System.out.println("The string is " + len + " characters long");
}
}
|
Before the new
operator did its work,
str1
was a "place holder" that did not actually refer to any object.
After the new
operator created the object,
str1
can be used to refer to the object.