Declaring and Using Methods

Methods are declared using the following syntax:

modifiers returnValue methodName(parameterList) throwsClause
{
 // Method body
}

A method can have any access modifier (public, protected, private, or package access) or the special modifiers (abstract, final, native, static, or synchronized). The use of these modifiers is covered later in this chapter.

The return type of a method can be any of the following:

  • void. The method does not return a value.

  • A primitive type. The primitive types boolean, byte, char, short, int, long, float, and double can be used as a method return type. The method must return a value that is promotable (refer to Chapter 3, “Operators and Assignments”) to the specified return type.

  • Object ...

Get Sun Certification Training Guide (310-025, 310-027): Java™ 2 Programmer and Developer Exams now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.