2.6 Method Calling: Application Programming: Java Programming
2.6 Method Calling: Application Programming: Java Programming
6 Method Calling
Application Programming: Java Programming
Outline
• What is a Method?
• Declaring a Method
• Method Calling
• Method Call Stack
• Parameter Passing
• Pass by Value
2
Objectives
• Define a method
3
What is a Method?
• A method refers to a piece of code referring to behaviors
associated either with an object or its class
4
Creating a Method
Steps in declaring a method class Number {
1. Set the return type
2. Provide method name int multiply(int i, int j) {
3. Declare formal parameters return i*j;
}
method signature
• consists of the method name and its
parameters int divide(int i, int j) {
• must be unique for each method in a class return i/j;
}
return statement
• allows the method to return a value to its double getPi() {
caller return 3.14159265358979;
• also means to stop the execution of the
current method and return to its caller }
• implicit return at the end of the method
void printSum(int i, int j) {
A method with empty parameters System.out.println(i+j);
}
A method that does not return a value must }
specify void as its return type
5
Method Calling
• How to call a method
1. Method name should match
2. Number of parameters should match
3. Type of parameters should match
6
Method Calling
public class JavaMain { class Person {
void talk() {
public static void main(String[] args) { System.out.println("blah, blah...");
// create a Person object }
Person you = new Person();
you.talk(); void jump(int times) {
you.jump(3); for (int i=0; i<times; i++) {
System.out.println(you.tellAge()); System.out.println("whoop!");
JavaMain.talkOnly(you); }
// create object of main program }
JavaMain me = new JavaMain();
me.jumpOnly(you); String tellAge() {
} return "I'm " + getAge();
}
static void talkOnly(Person p) {
p.talk(); int getAge() {
} return 10;
}
void jumpOnly(Person p) { } blah, blah...
p.jump(2); whoop!
}
whoop!
}
whoop!
I'm 10
blah, blah...
whoop!
whoop!
7
Method Call Stack
• The Method Call Stack refers to all the methods currently active
and being processed by a Java application
8
Passing Parameters
Passing parameters in Java is always Pass by Value!
9
Passing Parameters
public class MySelf { void changeAge(int age) {
age += 5;
public static void main(String[] args) { System.out.println("I imagine me being " +
int age=18; age);
int[] stats = {30,22,33}; }
String name = "Mary Jane";
MySelf me = new MySelf(); void changeStats(int[] stats) {
stats[0]=36; stats[1]=24; stats[2]=36;
System.out.println("I am just " + age); System.out.println("I wish I'm sexy at " +
me.changeAge(age); getStats(stats));
System.out.println("Nope, I'd rather be " }
+ age);
Same age String getStats(int[] stats) {
System.out.println("I hate my body at " + String s="";
me.getStats(stats)); for (int i=0; i<stats.length; i++) s +=
me.changeStats(stats); stats[i] + " ";
System.out.println("Wow! I'm now " + return s;
me.getStats(stats)); }
Changed stats
System.out.println("My name is so naive, " void changeName(String name) {
+ name); String newName = "MJ";
me.changeName(name); name = newName;
System.out.println("Naah, I still like " + System.out.println("What about " + name +
name); "?"); I am just 18
} Same name } I imagine me being 23
} Nope, I'd rather be 18
I hate my body at 30 22 33
I wish I'm sexy at 36 24 36
Wow! I'm now 36 24 36
My name is so naive, Mary Jane
What about MJ? 10
Naah, I still like Mary Jane
Key Points
• A method refers to what an object or class can do
• A method must have a return type, a name, and optional parameters
• The method signature refers to a method name and its parameters
• Return statement returns a value to its caller or returns control to its
caller
• A method that does not return a value must specify void as a return
type
• Calling a method should match its method signature
• When calling a method in the same class, use only the method name
• When calling a method in a different class, use the object reference
• When calling a static method, use the class name
• Methods are invoked sequentially in the call stack and executed in
reverse order
• Passing parameters in Java is always pass by value
11
Questions and Comments
12