Before and After

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Of course. In analogy:

object        you
reference     your cell phone number
variable      piece of paper with your cell phone number written on it
Boss Object

Before and After

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. This is shown on the right. 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.

QUESTION 7:

The object now exists. Can we run its methods?