Defining a Method

Purpose:

To define a capability for a class.

Mechanics:

<modifiers> <return types> <method name>() {

/* statements */
}

Example:

private void myMethod() {

/* statements */
}

Usage:

  • <modifiers> is an optional space-separated list of valid modifiers.
  • <return type> is either void (meaning that this method does not return a value), a name of a class, or a base type.
  • <method name> is any valid identifier which is unique to the class. It names a block of statements which, when executed in order, define a behavior for the class.

Restrictions:

A constructor is a special method that establishes the intial state for an object instance. It is denoted by not declaring a return type (which is different from declaring the return type as void) and by having a method name which is identical to the class name. Any method which is not a constructor must declare a return type.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu