go to previous page   go to home page   go to next page hear noise

Answer:

No. Just because there is a name for an object does not mean an object exists. Ask my pet unicorn Figgi to explain, if this is not clear.


Objects and Names for Objects


class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // str1 is a variable that may refer to an object. 
                   // The object does not exist unit the "new" is executed.
    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");
  }
}

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.

Before the new operator does its work, str1 is a "place holder" that does not yet refer to any object. After the new operator creates the object, str1 refers to that object.

This is similar to how names and objects work with real world things. I could speak of my dog "Rex" (a name for an object). Sadly, there is no such actual object. However, I could go to the dog pound and bring home a new dog (instantiating an object) and now "Rex" refers to that object.


QUESTION 8:

What object is referred to in the statement:

                                                          
len  = str1.length();  // invoke the object's method length()