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



Running a Method

Here is the example program, now with additional statements:

class StringDemo2
  public static void main ( String[] args )
    String str;
    int    len;

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

    len = str.length();

    System.out.println("The length is: " + len );


The expression


runs the length() method of the object referred to by str. This method counts the number of characters in the data of the object. In our object, it counts the number of characters in "Elementary, my dear Watson!" which is 27. Space characters and punctuation characters count in the length of a string. That value is then assigned to the int variable len.

Calling a method means asking a method to run. This program called the length() method. (Sometimes the phrase invoking a method is used to mean the same thing.)


Complete the last line of of the analogy:

object        you

reference     your cell phone number

variable      piece of paper with your cell phone number written on it

method call   ???