Adding new methods

So far we have used methods from Java libraries, but it is also possible to add new methods. We have already seen one method definition: main. The method named main is special, but the syntax is the same for other methods:

  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 Java keyword. By convention, Java methods start with a lower case letter and use “camel caps," which is a cute name for jammingWordsTogetherLikeThis.

The list of parameters specifies what information, if any, you have to provide to use (or invoke) the new method.

The parameter for main is String[] args, which means that whoever invokes main has to provide an array of Strings (we'll get to later). 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 mean that it takes no parameters. It contains one statement, which prints an empty String, indicated by "". Printing a String with no letters in it may not seem all that useful, but println skips to the next line after it prints, so this statement skips to the next line.

In main we invoke this new method the same way we invoke Java methods:

  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 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 more than once.
  • You can have one method invoke another method. In this case, main invokes threeLine and threeLine invokes newLine.
  • 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). It is usually a good idea to put each statement on its own line, but I sometimes break that rule.

You might wonder why it is worth the trouble to create all these new methods. There are several reasons; this example 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 statement, 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, to print nine consecutive new lines, you could invoke threeLine three times.

Later we will list some additional benefits of dividing programs into methods.