Introduction
A method is a collection of statements. The colletion of statements is given a name. The statements of the method are run by invoking the name of the method. The code below illustrates the functioning of a method named first, which is contained in the class Demo. The other class, showMethods, is only there to create a Demo object and run Demo's first method.
There is a flow, or sequence, of instructions that get performed when you run a Java program. Below, the flow is indicated by the numbers in the comments (i.e., the statement with the comment "1" is the first instruction run by the CPU, the statement with the comment "2" is the second instruction to be performed, etc.).
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo(); // 1
dm.first();// 2
System.out.println("Done.");// 6
}
}
class Demo
{
void first( )
{
System.out.println("In"); // 3
System.out.println("first");// 4
System.out.println("method");// 5
}
}
The output of the above program is:
In
first
method
Done.
Click here for a discussion of the above program (the discussion is a Flash document which requires a recent version of Macromedia's free Flash player). Note that this video includes audio -- make sure that your speakers are enabled so that you can here the soundtrack.
One Method Called Several Times
You can repeatedly call a method:
public class ShowMethods2
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
dm.first();
System.out.println("Well, that was interesting.");
dm.first();
}
}
class Demo
{
void first( )
{
System.out.println("In");
System.out.println("first");
System.out.println("method");
}
}
The program shown above will generate the following output:
In
first
method
In
first
method
Well, that was interesting.
In
first
method
Breaking A Method Into Two Methods
If you have a large number of statements in a method, such as the nine statements shown in the first method, below
public class ShowMethods3
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
statement1;
statement2;
statement3;
statement4;
statement5;
statement6;
statement7;
statement8;
statement9;
}
}
you can move some of those statements to another method. In the updated version of the program shown below (ShowMethods4), statements one through four have been moved to a new method named sample. The version of the program shown below (ShowMethods4) will run through the same sequence of statements -- statements one through nine -- as the version of the program shown above (ShowMethods3).
public class ShowMethods4
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
sample();
statement5;
statement6;
statement7;
statement8;
statement9;
}
void sample()
{
statement1;
statement2;
statement3;
statement4;
}
}
Putting a group of statements into a separate method is especially useful if there are groups of repeated statements. In the example below, the statements 1 through 4 are repeated three times.
public class ShowMethods5
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
statement1;
statement2;
statement3;
statement4;
statement5;
statement6;
statement1;
statement2;
statement3;
statement4;
statement7;
statement8;
statement9;
statement1;
statement2;
statement3;
statement4;
}
}
Below, the repeated statements are pulled in to a separate method named common and written down once in that method. The common method is then called three times:
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
common();
statement5;
statement6;
common();
statement7;
statement8;
statement9;
common();
}
void common()
{
statement1;
statement2;
statement3;
statement4;
}
}
Having only one written copy of the repeated set of statements is especially useful if the statements occasionally need to be modified.
Passing parameters
You can pass values in to a method. A value passed to a method is referred to as a "parameter".
public class ShowMethods6
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first()
{
doubleIt(167); // here we are "calling" the doubleIt method
doubleIt(31);
doubleIt(49);
}
void doubleIt(int val) // doubleIt is referred to as the "called" method
{
int twice;
twice = val * 2;
System.out.println("Twice " + val + " is " + twice);
}
}
The doubleIt method, above, is called three times. Each time it is sent a different value. The following output is generated when the program is run:
Twice 167 is 334
Twice 31 is 62
Twice 49 is 98
Each time the doubleIt method is called it is sent (passed) the value between the parantheses. That value is received by the doubleIt method and placed into the variable val. This is very similar to an assignment statement -- the value is getting assigned from where the method is called (where we are sending down the value 167 for the first method call) into the variable val in the called method doubleIt.

The doubleIt method, above, receives a single parameter. A method can receive
multiple parameters, such as the doubleTwoNums method below. The first passed value goes into the first variable in the called method, the second value sent goes into the second variable. The first time doubleIt is called 40 is placed into valOne and 16 is placed into valTwo.
public class ShowMethods7
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first()
{
doubleTwoNums(40, 16);
doubleTwoNums(6, 31);
doubleTwoNums(800, 42);
}
void doubleTwoNums(int valOne, int valTwo)
{
int twice1, twice2;
twice1 = valOne * 2;
twice2 = valTwo * 2;
System.out.println("Doubling " + valOne + " and " + valTwo);
System.out.println("Twice " + valOne + " is " + twice1);
System.out.println("Twice " + valTwo + " is " + twice2);
System.out.println();
}
}
The output of the above application is:
Doubling 40 and 16
Twice 40 is 80
Twice 16 is 32
Doubling 6 and 31
Twice 6 is 12
Twice 31 is 62
Doubling 800 and 42
Twice 800 is 1600
Twice 42 is 84
Return Values
The examples shown above show the typical, or primary, purpose of a
method: to collect together some statements and give that collection of
statements a name. However, a method has a second behavior that can be
used (or ignored) -- a method can be given a value, which can
be used like any other value. The value can be a simple built-in type such as int or
double, or it can be a reference to an object. Here, we'll just
look at methods return simple numeric values.
public class ShowMethods8
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first()
{
int w;
w = giveVal();
System.out.println("Value is: " + w);
}
int giveVal()
{
return 753;
}
}
The giveVal method above returns the int value 753. The call to the method, giveVal(), returns, or gives out, the value 753. This int value can be used like any other integer -- in the code above the 753 is simply assigned to the integer variable w.
It is more interesting if the method calculates a value to return, versus just returning the exact same value everytime the method is called:
public class ShowMethods9
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first()
{
double num;
num = giveSquare(5.9);
System.out.println("Value is: " + num);
}
double giveSquare(double inVal)
{
double squaredVal;
squaredVal = inVal * inVal;
return squaredVal;
}
}
The program above generates this output:
Value is: 34.81
Of course, a value returning method can be called multiple times:
public class ShowMethods10
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first()
{
double num;
num = giveSquare(5.9);
System.out.println("Value is: " + num);
num = giveSquare(161.35);
System.out.println("Value is: " + num);
}
double giveSquare(double inVal)
{
double squaredVal;
squaredVal = inVal * inVal;
return squaredVal;
}
}
The above program gives the following output:
Value is: 34.81
Value is: 26033.8225
If a method is not going to return any value, its type is declared as void.
Methods -- Scope
The scope of a variable
The scope of a variable is the set of instructions that can see that variable. A variable's scope is limited to the nearest set of enclosing curly braces: {}. The following examples look at the scope of an integer variable x. The scope of x is indicated by the code that is printed in
bold font.
- In this example the variable x is "local to", or contained within, the method book, so x's scope is limited to the instructions inside the book method. Since the scope of x is limited to the book method, it cannot be seen or used by instructions found anywhere else in the program (the instructions in first cannot refer to x, and the instructions in main cannot refer to x).
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
statement1;
statement2;
book();
statement3;
}
void book()
{
int x = 75; // x is enclosed by the book method's {}s
statement4;
statement5;
statement6;
}
}
- With regard to scope, a method's parameters are considered to be local to (enclosed within) the body of the method. Therefore, the scope of the parameter x, below, is limited to the book method:
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
statement1;
statement2;
book(165);
statement3;
}
void book(int x)
{
statement4;
statement5;
statement6;
}
}
- Here, x is local to the first method:
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
int x = 75;
statement1;
statement2;
book();
statement3;
}
void book()
{
statement4;
statement5;
statement6;
}
}
- In this example the nearest set of curly braces that enclose the x variable are the curly braces that define the boundaries of the Demo method. Since those curly braces also contain all of the methods of the Demo class, all of those methods can see the x variable.
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
int x = 75;
void first( )
{
statement1;
statement2;
book();
statement3;
}
void book()
{
statement4;
statement5;
statement6;
}
}
A variable can be declared in two different locations. However, each declaration of the variable creates a separate instance of the variable, which can be confusing, since you will have two different values residing under the same name. In the example below, the code in italics can see the first x (and the value 35 that it holds) and the bold code can see the second x (and the value 6 that it holds):
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
void first( )
{
int x = 35;
statement1;
statement2;
book();
statement3;
}
void book()
{
int x = 6;
statement4;
statement5;
statement6;
}
}
It is even possible to have the following situation:
public class ShowMethods
{
public static void main(String [] args)
{
Demo dm = new Demo();
dm.first();
}
}
class Demo
{
int x = 57;
void first( )
{
int x = 35;
statement1;
statement2;
book();
statement3;
}
void book()
{
statement4;
statement5;
statement6;
}
}
In the case shown above, the scope of the x that contains 57 extends over the entire contents of the Demo object -- all of the instructions in all of the methods can see that x. This overlaps with the scope of the x with the value 35 declared in the first method. If there is a situation where the scope of two variables with the same name overlap, then the most local scope "wins" (for a given instruction, the variable declared within the nearest set of enclosing curly braces is the variable that the instruction sees). In the example above, an instruction in the first method, such as statement2, is within the scope of both x variables -- statement2 will see the x variable defined in the first method, with its value of 35 since the curly braces that enclose the x with 35 are closer than the instructions that contain the x with 57. The statements in book, such as statement5, will see the x containing 57 since the curly braces of the Demo class are the nearest set of curly braces that enclose both statement5 and an x variable.
Here is a discussion about the scope of variables declared inside objects and methods. |