Logout

Methods Introduction

 1 package methodspackage;
 2 
 3 public class MethodsTeaching {
 4     public static void main(String[] args) {
 5         juggle(3, "candles", true);
 6         jump();
 7         jump();
 8         jump();
 9         juggle(7, "ib subjects", true);
10     }
11     public static void juggle(int numThings, String thing, boolean wantToJuggle){//the "header" or "signature" of the juggle method
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 jump(){//note that in this method signature no parameters are taken in
18         System.out.println("You want to jump!!");
19     }
20 
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. For starters, you can reuse 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.

But more importantly, be using methods, we break up the code into manageable, discrete sections, which we can focus on. Think of it as a "divide and conquer" approach. This fact is particularly important vis-a-vis our ability to debug programs; we can isolate our problems to a given method, and fix it without tampering with other (functional) parts of the program. The other thing about methods is that their signature is the "public interface" that other classes can use (more on this later).


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.

aMethod, above is the variable name. It can be any legal combination of characters just like a variable name. And like variable names, we, by convention, 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 "Implementation"

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 "implement", we mean construct. So we implement methods when we create them.

 

Link to the Methods Notes Part 2