Chapter 3

Programming in the Large I
Subroutines


ONE WAY TO BREAK UP A COMPLEX PROGRAM into manageable pieces is to use subroutines. A subroutine consists of the instructions for carrying out a certain task, grouped together and given a name. Elsewhere in the program, that name can be used as a stand-in for the whole set of instructions. That is, as a computer executes a program, whenever it encounters a subroutine name, it executes all the instructions necessary to carry out the task associated with that subroutine.

Subroutines can be used over and over, at different places in the program. A subroutine can even be used inside another subroutine. This allows you to write simple subroutines and then use them to help write more complex subroutines, which can then be used in turn in other subroutines. In this way, very complex programs can be built up step-by-step, where each step in the construction is reasonably simple.

In Java, any subroutine you write has to be part of a class or object. Such subroutines are called methods. A static method is part of a class, rather than part of the objects defined from that class. Static methods correspond directly to subroutines in traditional, non-object oriented programming languages. This chapter deals with the more traditional type of subroutines. Methods that belong to objects, the so-called instance methods, will be covered in the next chapter.


Sections in Chapter 3:

  1. Black Boxes
  2. Static Methods and Static Variables
  3. Parameters
  4. Return Values
  5. Toolboxes, API's, and Packages
  6. More on Program Design

[ First Section | Next Chapter | Previous Chapter | Main Index ]