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


Of course. This is like an object reference variable that does not (yet) hold an object reference.

Before and After

Here is the statement we are considering:

str = new String( "Elementary, my dear Watson!" );

And here are pictures of the action.

The picture on the left shows the program just as it starts to run. No objects have been created yet, but the reference variable exists. The slash through the variable means that it does not yet refer to an object. Then the assignment statement executes:

str = new String( "Elementary, my dear Watson!" );

This creates an object, fills it with data, and puts a reference to it in str.

The picture on the right shows the program after the assignment statement has run. The reference is shown as an arrow that leads to the object, and this is a good way to think of it. (Although in reality it is just a bit pattern, like everything else in computer memory.)

object creation

The variable will continue to hold the reference to the object until some other assignment statement changes it or the program ends.


The object now exists. Can you run its methods?