By Barry Burd

Understanding the Java method is like understanding any other task you might carry out in your daily life. Suppose you’re working as an auto mechanic in an upscale garage. Your boss, who’s always in a hurry and has a habit of running words together, says, “fixTheAlternator on that junkyOldFord.”

Mentally, you run through a list of tasks. “Drive the car into the bay, lift the hood, get a wrench, loosen the alternator belt,” and so on. Three things are going on here:

  • You have a name for the thing you’re supposed to do. The name is fixTheAlternator.

  • In your mind, you have a list of tasks associated with the name fixTheAlternator. The list includes “Drive the car into the bay, lift the hood, get a wrench, loosen the alternator belt,” and so on.

  • You have a grumpy boss who’s telling you to do all this work. Your boss gets you working by saying, “fixTheAlternator.” In other words, your boss gets you working by saying the name of the thing you’re supposed to do.

In this scenario, using the word method wouldn’t be a big stretch. You have a method for doing something with an alternator. Your boss calls that method into action, and you respond by doing all the things in the list of instructions that you associate with the method.

If you believe all that, then you’re ready to read about Java methods. In Java, a method is a list of things to do. Every method has a name, and you tell the computer to do the things in the list by using the method’s name in your program.

No one has probably ever written a program to get a robot to fix an alternator. But, if they did, the program might include a fixTheAlternator method. The list of instructions in the fixTheAlternator method would look something like the text in this listing.

Don’t scrutinize the listings too carefully. All the code is fake! It looks a lot like real Java code, but it’s not real. What’s more important, the code isn’t meant to illustrate all the rules about Java.

void fixTheAlternator() {
   driveInto(car, bay);
   lift(hood);
   get(wrench);
   loosen(alternatorBelt);
   ...
}

Somewhere else in the Java code (somewhere outside of the listing shown here), you need an instruction to call the fixTheAlternator method into action. The instruction to call the fixTheAlternator method into action may look like the line in this listing.

fixTheAlternator(junkyOldFord);

Now that you have a basic understanding of what a method is and how it works, you can dig a little deeper into some useful terminology:

  • You can refer to the code as a method or as a method declaration.

  • The method declaration has two parts. The first line (the part with fixTheAlternator in it, up to but not including the open curly brace) is a method header. The rest of the listing (the part surrounded by curly braces) is a method body.

  • The term method declaration distinguishes the list of instructions from the instructions known as a method call.

A method’s declaration tells the computer what happens if you call the method into action. A method call (a separate piece of code) tells the computer to actually call the method into action. A method’s declaration and the method’s call tend to be in different parts of the Java program.