Class: Defining Methods

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}
  • The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.
  • More generally, method declarations have six components, in order: 1. Modifiers—such as public, private, and others you will learn about later. 2. The return type—the data type of the value returned by the method, or void if the method does not return a value. 3. The method name—the rules for field names apply to method names as well, but the convention is a little different. 4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. 5. An exception list—to be discussed later. 6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
  • Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized.

Class: Defining Methods — Structure map

Clickable & Draggable!

Class: Defining Methods — Related pages: