Quick Index
Accompanying Videos



Overview

We've touched upon method header previously. We'll now dig deep into the subject and establish our expertise.

The purpose of the "method header" is to provide details to potential callers of the method.

It would be very inefficient to require callers to dig through many lines of code to determine what a method does. Instead, the method caller need only look at the method header.

Note that another name you will hear for method header is "method signature".

This may be the most important chapter.

What Is The Method Header?


The method header is highlighted.

It is the first line of the method and gives important information about the method.
public void setDimensions(int aWidth, int aHeight) {
	width = aWidth;
	height = aHeight;
}

Method Header Components (The Big Three)


Here is an accompanying video.

  • Method Return Type -- means the method returns a value with the given return type ("int" in this case)
  • Method Name -- the name of the method
  • Method Parameters -- the method parameters that must be passed by the sender to this method (in this case two params -- both of type int)

The method header along with the method body make up the syntax of the method.

The access modifier "public" means available for use by other objects
The method parameters list may be of size zero to many


Example call of method:

 int m = object.max(8, 5);
public 
1
int
2
max(
3
int a, int b)

Java method may or may not return a value.

We use the term "void" on the method header to indicate no return value.

Example call of method:

 object.close();
public void close()
This method header tells us that this method receives one parameter of type "int".

Note that we give the parameter the name "amount". That is our choice -- we could call it "a", "sally", etc (whatever we like).
we can use whatever name we like for the parameter (like any variable). The caller does not care.


Example call of method:

 object.growBy(15);
public void growBy(int amount)
This method header tells us that it receives two parameters:

  • The first parameter named "aWidth" of type "int"
  • A second parameter named "aHeight" of type "int"

Example call of method:

 object.setDimensions(10, 5);
public void setDimensions(
1
int aWidth,
2
int aHeight)

With Mixed Parameters


Parameters may be of different types. This method expects three parameters of types: String, int, boolean (in that order).

public String write(String title, int linesPerPage, boolean useBackground)


Example call of method:

 String report = object.generateReport("Annual Fun Report", 80, true);


With Object Return Value


This method header tells us that the method returns an object of type "Ball".

Return types may be simple like "int" as we have seen, but can also be any object type (class) as well (Ball, Rectangle, Person, Point, etc)

Example call of method:

Ball redBall;
redBall = object.findRedBall();
public Ball findRedBall()

With Object Method Parameters


This method header tells us that three parameters will be sent to this method of types: Ball, Rectangle, int.

public void initializeAll(Ball b, Rectangle r, int defaultValue)


Example call of method:

Ball ball = new Ball();
Rectangle rec = new Rectangle();
object.initializeAll(ball, rec, 10);



Ball ball = new Ball();
Rectangle rec = new Rectangle();
object.initializeAll(ball, rec, 10);