Objects are Created at Run Time

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:

    str = new String( "You know my methods, Watson.!" );

Objects are Created at Run Time

Before the program runs, there is no object. The new String object is created as the program runs.

class StringDemo1
{
  public static void main ( String[] args )
  {
    String str;

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

  }
}

The declaration

String str; 

creates a reference variable, but does not create a String object. The variable str is used to refer to a String after one has been created. The next statement

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

is an assignment statement that creates an object and puts a reference to that object in str.

After the program stops running, the String object no longer exists. Its memory is reclaimed by the computer system for other uses.

QUESTION 5:

(Review: ) What are the two steps in an assignment statement?