Java Methods
The word method in Java is essentially the same thing as the word
function used in other languages like C and C++. A method is a
function or subroutine. Every method in Java belongs to a class (this is
like the C++ member function).
Why Write Methods?
Divide-and-conquer: Break programs into smaller, more manageable
pieces
Reusability
Avoid repetition of code in a program
Building Methods
The structure of a method:
modifier(s) returnType methodName(parameter list)
{
// method body (i.e. what it does, how it works)
}
The pieces:
- methodName - identifier chosen by the builder
- parameter list - a comma-separated list of the parameters that
the method will receive. This is data passed IN to the method by the
caller. The parameter list indicates the types, order, and number of
parameters
- returnType - the data type of the value returned by the
method. A method that returns no value should have return type
void
- modifier(s) - optional labels that can specify certain
properties or restrictions on the method
- (For now, we will use the modifier static on our
methods)
- method body - code statements that make up the definition of
the method and describe what the method does
To return a value (from the body of a method with a non-void return
type), use the keyword return:
return expression;
A return statement will force immediate exit from the method, and
it will return the value of the expression to the caller.
Note: A method with a non-void return type needs to return an
appropriate value.
Calling Methods
A call is a command to execute a method, which transfers program
execution to the code inside the method body. A call must use the method
name and must pass in appropriate arguments, matching the expected number
and types of parameters in the parameter list.
Examples:
A method with void return type:
static void printGreeting(int age)
{
System.out.println("Hello, my name is Johnny");
System.out.println("And I am " + age + " years old");
}
Sample calls:
printGreeting(6); // can pass in a literal number
int x = 10;
printGreeting(x); // can pass in a variable of matching type
Note: For a method with return type void, simply call the
method as a single statement, ending with a semicolon.
A method with non-void return type:
static double Compute(int x, double y)
{
double result;
result = x * 1.5 + y;
return result;
}
Sample calls:
double ans;
ans = Compute(5, 3.6); // returned value is assigned to ans
// in this sample call, the returned value is printed by the
// System.out.print statement
int a = 16;
double b = 3.4;
System.out.print("Answer is " + Compute(a,b));
Note: For a function with non-void return type, use the returned
value by placing the function call in the middle of another statement,
where that tye of value is valid. For example, a returned value can be
assigned to a variable. The returned value replaces the call when
evaluating what happens next in a statement with multiple operations.
Scope
Scope - the portion of code in which a variable is valid.
- a variable declared inside a block { } of executable code has scope
from the point of its delcaration to the end of the block
- Note that this means any variable declared inside a method body has
scope only in that method. Often called a local variable
- A variable declared as a formal parameter of a method also has local
scope (only within that method).
Pass By Value
- Default mode of passing parameters into methods
- Means that the parameter inside the method body is a copy of
the original argument that was passed in
- Changes to the local parameter only affect the local copy, not the
original argument in the call
Example
Method:
static int myMethod(int x, int y)
{
x = x * 2;
System.out.println("x = " + x);
y = y * 2;
System.out.println("y = " + y);
return x + y;
}
Sample call:
int a = 5, b = 8, ans;
ans = myMethod(a, b);
System.out.println("ans = " + ans);
System.out.println("a = " + a);
System.out.println("b = " + b);
Notice that the output of the code is:
x = 10
y = 16
ans = 26
a = 5
b = 8
Try this code yourself, here