next up previous contents
Next: Passing information between methods Up: 6 Multiple methods Previous: 6 Multiple methods   Contents

Basics of using another method

So far all the source files you have written have had the same basic structure. Firstly a public class for which you choose the name, the contents of which are inside a set of curly brackets. Secondly, inside the class, a public static void method which must be called main, and must have the argument String[] argv. Again the contents of the main method are contained within a set of curly brackets {}.

/**
 * Purpose: To show most simple program structure.
 */

public class Simple
{
    public static void main(String[] argv)
    {
        //some program code goes here
    }
}

We are now going to add to this program structure a second method. See that this second method is still inside the class, but unlike all the other code you have made use of so far, it is outside the main method.

/**
 * Purpose: To show structure of program with multiple methods.
 */

public class LessSimple
{
    // another method - can be called what you like
    public static void doSomething()
    {
        //code may go in here in the usual way - variables, ifs, loops etc.
        //must end with a return statement...
        return;
    }

    // main method as usual
    public static void main(String[] argv)
    {
        //some program code goes here
        //should use the doSomething() method
        //this is done by typing
        doSomething();
    }
}

The lines doSomething() and return will be explained shortly.

When the program is run, the main method is executed. Any other methods that exist will only be executed if they are used by the main method. The main method can be thought of as the top level of the program, from which other methods are invoked. It is good practice to keep main very short and simple, by splitting your program up into different methods which can be called from main.

The convention is to have the main method written as the last method in your source file, just so that it is easy to find. The order in which methods are written doesn't affect the order in which they are executed -- even though it is at the end, the computer always begins by executing the main method.

Note that the additional method is declared using public static void just as for the main method.

Here is a simple example with exactly the same structure as above.

/**
 * Purpose: Prints the first 10 square integers,
 *          as an example to show the use of two methods.
 */

public class Trivial
{
   // method to print squares from one to a hundred
   public static void squares()
   {
       for(int i = 1; i < 11; i++)
       {
           System.out.println(i*i);
       }
       return;
   }

   // main method 
   public static void main(String[] argv)
   {
       squares();      // calls the squares method
   }

}

The line of code squares(); is where the main method makes use of the squares method, asking it to carry out its set of instructions. This is known as a call to the squares method. The return statement at the end of squares sends flow of the program back to the method that called it, and so execution of the main method continues.

You may have found that writing programs where all your code is within the main method can become very long, which makes the source file look messy or unclear. It can also be awkward if you have similar groups of calculations or instructions that need to be performed and need to be typed in separately each time. Using additional methods can help with both of these issues, making code tidier and therefore easier to understand, and reducing the need to duplicate code. There is little obvious advantage in having separated the above program into two methods -- the same could very easily be achieved with just a main method -- it is just a simple example in how to write and call methods. However with larger programs there are significant benefits of separating your code into several methods in this way.


next up previous contents
Next: Passing information between methods Up: 6 Multiple methods Previous: 6 Multiple methods   Contents
RHUL Dept. of Physics