Home Topic 4

Methods Introduction

 1 package methodspackage;
 3 public class MethodsTeaching {
 4     public static void main(String[] args) { 
 5         juggle(3, "candles");
 6         sing();
 7         sing();
 8         sing();
 9         juggle(7, "ib subjects");
10     }
11     public static void juggle(int numThings, String thing){//in this "header", parameters taken in
12         System.out.println("You want to juggle " + numThings + " " + thing + ".");
13         if(numThings > 6 && thing.equals("ib subjects")){
14             System.out.println("You're crazy to take so many IB subjects!!!!");
15         }
16     }
17     public static void sing(){//in this header, no parameters taken in
18         System.out.println("You want to sing!!");
19     }
21 } 


Another Way to Control Flow: Methods

Recall that in Java, flow of control is not simply straight down through the code one line after another (which actually is the case in some simple languages).

The first thing you can note in the above code is that the flow of control is, at one point, determined by an if block. If the numThings is greater than 6, and the thing is "ib subjects", then line 13 is followed by 14 and 15, otherwise line 13 skips to line15. We have seen before how flow of control can be altered this way, with conditional blocks.

But the flow of control above is also determined in this program by "method calling". The trace would go lines would actually go 1, 2, 3, 4, 5, and then to 11, 12, 13 (and maybe 14 and 15), 16, and then back to line 6, down to line 17, and so on.

Why Methods?

There are many advantages to using methods.

(1.) Code reuse - we can use the same code over and over again, without having to copy and paste. Above, we call the juggle( ) code twice, and the jump( ) method three times, but yet the method is only implemented once.

(2.) Modularity - we can break up the code into manageable, discrete sections, which are:

(3.) Abstraction - This is a key example of abstraction, in which we can focus on what we need to focus on, and ignore the other details.

(4.) Easier debugging - through modularity and abstraction, we can isolate our problems to a given method, focus in on them, and fix them without tampering with other parts of the program.

We often refer to such an overall modularity approach as being one of "divide and conquor".


All of this can be succincly summarized in a sentence, as the curriculum puts it:

Sub-programmes and objects support abstraction, which facilitates: ease of debugging and maintenance, reuse of code, modularity.

The Structure of a Method - Part 1

There will be one more piece to add to this soon, hence the "Part 1", but for now, let's look at how a method is structured, taking into account all but the void part. Here is sample full method header:

public void aMethod(String aParameter)

public means that other parts of the program, as well as other classes can access and use this method.

void is the part we'll get back to, but basically it means the method is void of a "return value".

aMethod, above is the method name. It can be any legal combination of characters just like a variable name. And like variable names, by convention we start it with a lower case letter, and capitalize any other words that make up the name.

String aParameter is a parameter, which is a piece of information that is needed to be "taken in" by the method for it to work properly. In our example at the top of the page, one of our methods (other than the main method) took in three parameters, and one of them took in no parameter.

Note that when there is more than one parameter, the parameters are separated by a coma.

By the way "parameter" and "argument" are often used interchangeably, but, really, the argument is the value sent to a method, and the parameter is the value received. So in the above code, the stuff in the parentheses of lines 5 and 9 are the arguments that are sent. And the parameters are the variables declared within the header of the juggle() method.

And the body of the method is between and open and closed brace - { } - as with other blocks of code.


Method "Calling" Versus Method "Definition"

A minor point regarding terminology should be made at this point. We say we "call" a method when, in our code we use a method which exists some place else, either in our code, or in a class which we have access to. So in our example at the top of the page, we call our own methods juggle( ) and jump( ) from within the main method, and we also call the println( ) method inside both of the methods which we implement in the above class.

So by "defining", or "implementing", we mean constructing, or making. So we define methods when we create them.


Link to the Methods Notes Part 2