Method definition skeleton

A method definition tells Java the name of a new method and how it works. The skeleton of a method definition is a first draft, essentially translating the contract into Java syntax but not specifying how the method does its job. It has the following form (for now; we'll see more details later):

public static return-type method-name
             (parameter-type parameter-name)
   {

   }
You can also write methods with multiple parameters:
public static return-type method-name
             (parameter-type parameter-name,
	      parameter-type parameter-name,
              ...)
   {

   }

In either case, the definition consists of several parts, each separated by a space (and anywhere you can put a space, you can also put a new line):

The first six of these are collectively called the header, and traditionally go on one line (unless there are too many parameters to fit on one line, in which case they may be one on each line), with the body (enclosed in curly-braces) starting on the next line.

Here's an example.

public static int cube (int num)
   {
   }
Note that the method-name and parameter-name can each be whatever you want, as long as each is a sequence of letters (and perhaps some punctuation marks, but no spaces). Typically the method-name is chosen to suggest what the function does, and each parameter-name is chosen to suggest the parameter's type or role.

Another example:

public static String howOld (String name, int age)
   {
   }


Last modified: Fri Jan 30 11:20:45 EST 2009
Stephen Bloch / sbloch@adelphi.edu