Developing and Using Methods
Developing and Using Methods
Objectives
Describe the advantages of methods and define
worker and calling methods
Declare and invoke a method
Compare object and static methods
Use overloaded methods
Creating methods
Syntax of method declaration
[modifiers] return_type method_identifier
([arguments]) { method_code_block }
Modifiers: keywords that modify the way methods are
used (optional)
return_type: only one item (type) or void
method_identifier: name of the method
([arguments]): optional list of variables
method_code_block: statements
Creating methods (2)
Basic form of a method accepts no arguments and
returns nothing
example
1 ...... V1 4 ......
2 ...... Value 1 being 5 ......
passed from object 1
3 ...... to object 2 6 ......
9 ...... 7 ......
10 .... 8 ....
V2
Object 2 returns
value 2 to object 1
Declaring methods with Arguments
Example
public void setFloor(int desiredFloor) {
while (currentFloor != desiredFloor) {
if (currentFloor < desiredFloor) {
goUp();
}
else {
goDown();
}
}
}
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goUp();
myElevator.goUp();
myElevator.goUp();
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goDown();
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goDown();
myElevator.setFloor(myElevator.TOP_FLOOR);
myElevator.openDoor();
}
}
Declaring methods with return values
void is used for methods that do not return a value
To declare a method that returns a value, place the
type of value in front of the method identifier
public int sum(int numberOne, int numberTwo)
A method can return only one value, but can accept multiple
arguments
To return a value from a method, use the return
keyword
public int sum(int numberOne, int numberTwo) {
int return = numberOne + numberTwo;
return result;
}
Receiving return values
If you invoke a method that returns a value you can
use the return value in the calling method
public class ElevatorTestTwo {
public static void main(String args[]) {
Elevator myElevator = new Elevator();
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goUp();
myElevator.goUp();
myElevator.goUp();
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goDown();
myElevator.openDoor();
myElevator.closeDoor();
myElevator.goDown();
System.out.println(“CurrentFloor: “ + curFloor);
myElevator.setFloor(curFloor + 1);
myElevator.openDoor();
}
}
Advantages of method use
Methods make programs more readable and easier to
maintain
Methods make development and maintenance quicker
Methods are central to reusable software
Methods allow separate objects to communicate and
to distribute the work performed by the program
Creating static methods and variables
}
}
Method overloading and the Java API
Many methods in the Java API are overloaded,
including the System.out.println method
Variations of the println method
void println()
void println(boolean x)
void println(char x)
void println(char[] x)
void println(double x)
void println(float x)
void println(int x)
void println(long x)
void println(Object x)
void println(String x)
Uses for method overloading
When you write code, keep in mind that you must
define overloaded methods if the actions the method
completes must be performed on several types of
data.
You can also use overloading to create several
methods with the same name but with a different
number of parameters.
public int sum(int numberOne, int numberTwo)
public int sum(int numberOne, int numberTwo, int
numberThree)
public int sum(int numberOne, int numberTwo, int
numberThree, int numberFour)
Invoking overloaded methods
The following example creates three object instances
of the ShirtTwo class and uses their overloaded
methods
public class ShirtTwoTest {
public static void main(String args[]) {
ShirtTwo shirtOne = new ShirtTwo();
ShirtTwo shirtTwo = new ShirtTwo();
ShirtTwo shirtThree = new ShirtTwo();
shirtOne.display();
shirtTwo.display();
shirtThree.display();
}
}