The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Adding New Methods

So far we have only been using the methods that are built into Java, but it is also possible to add new methods. Actually, we have already seen one method definition: main. The method named main is special in that it indicates where the execution of the program begins, but the syntax for main is the same as for any other method definition:

  public static void NAME ( LIST OF PARAMETERS ) {
    STATEMENTS
  }

You can make up any name you want for your method, except that you can't call it main or any other Java keyword. The list of parameters specifies what information, if any, you have to provide in order to use (or invoke) the new function.

The single parameter for main is String[] args, which indicates that whoever invokes main has to provide an array of Strings (we'll get to arrays in Chapter 10). The first couple of methods we are going to write have no parameters, so the syntax looks like this:

  public static void newLine () {
    System.out.println ("");
  }

This method is named newLine, and the empty parentheses indicate that it takes no parameters. It contains only a single statement, which prints an empty String, indicated by "". Printing a String with no letters in it may not seem all that useful, except remember that println skips to the next line after it prints, so this statement has the effect of skipping to the next line.

In main we can invoke this new method using syntax that is similar to the way we invoke the built-in Java commands:

  public static void main (String[] args) {
    System.out.println ("First line.");
    newLine ();
    System.out.println ("Second line.");
  }

The output of this program is

First line.

Second line.

Notice the extra space between the two lines. What if we wanted more space between the lines? We could invoke the same method repeatedly:

  public static void main (String[] args) {
    System.out.println ("First line.");
    newLine ();
    newLine ();
    newLine ();
    System.out.println ("Second line.");
  }

Or we could write a new method, named threeLine, that prints three new lines:

  public static void threeLine () {
    newLine ();  newLine ();  newLine ();
  }

  public static void main (String[] args) {
    System.out.println ("First line.");
    threeLine ();
    System.out.println ("Second line.");
  }

You should notice a few things about this program:

  • You can invoke the same procedure repeatedly. In fact, it is quite common and useful to do so.
  • You can have one method invoke another method. In this case, main invokes threeLine and threeLine invokes newLine. Again, this is common and useful.
  • In threeLine I wrote three statements all on the same line, which is syntactically legal (remember that spaces and new lines usually don't change the meaning of a program). On the other hand, it is usually a better idea to put each statement on a line by itself, to make your program easy to read. I sometimes break that rule in this book to save space.

So far, it may not be clear why it is worth the trouble to create all these new methods. Actually, there are a lot of reasons, but this example only demonstrates two:

  1. Creating a new method gives you an opportunity to give a name to a group of statements. Methods can simplify a program by hiding a complex computation behind a single command, and by using English words in place of arcane code. Which is clearer, newLine or System.out.println ("")?
  2. Creating a new method can make a program smaller by eliminating repetitive code. For example, how would you print nine consecutive new lines? You could just invoke threeLine three times.


Last Update: 2011-01-24