Introduction to methods

A method brings together in one place one or more behaviors that can be executed by a program.

The most fundamental distinction concerning methods is that between defining them and calling (executing) them. Programs include both method definitions (also called declarations) and method calls. Here is a simple method definition:

public void step() {
    turn(1);
    paint();
}
Here is a simple method call:
turn(1);

A method call is an example of a statement, something that the program is to execute. Statements must be followed by a semicolon.

Behaviors that a method executes include

There are two basic kinds of methods in Java, static methods, which "belong" to a whole class when they are called, and instance methods (or non-static methods), which "belong" to an individual object when they are called. It is instance methods that are the essence of Java, and object-oriented, programming, and most of a program consists of these. But for now we will be working only with static methods.

To call a method in a class other than where it is defined, you need to precede the identifier for the method with something that tells it where to find the method followed by . For static methods, this just the name of the class. For example, Beh.turn().

Home

Calendar

Coursework & grading

Assignments

Lecture notes

Other resources


IU home

IU CS home

Contact instructor