The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Classes, Interfaces, and Packages

## Implementing Methods

> This figure shows the code for `Stack`'s push method. This method pushes an object, the one passed in as an argument, onto the top of the stack, and returns it.

> Like a class, a method has two major parts: method declaration and method body. The method declaration defines all of the method's attributes, such as access level, return type, name, and arguments, as illustrated here:

> The method body is where all the action takes place. It contains the Java instructions that implement the method.

### The Method Declaration

> At minimum, a method declaration has a name and a return type indicating the data type of the value returned by the method:

> >     returnType methodName() {
> >         . . .
> >     }

> This method declaration is very basic. Methods have many other attributes such as arguments, access control, and so on. This section will cover these topics as well as expand upon the features illustrated in the method declaration above.

### Passing Information into a Method

> Perhaps the most commonly used optional component of a method declaration are method parameters. Similar to functions in other programming languages, Java methods accept input from the caller through its parameters. Parameters provide information to the method from outside the scope of the method.

### The Method Body

> The method body is where all of the action of a method takes place; the method body contains all of the legal Java instructions that implement the method.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form