[go: up one dir, main page]

0% found this document useful (0 votes)
45 views12 pages

2.6 Method Calling: Application Programming: Java Programming

This document discusses Java methods, including defining methods, calling methods, and parameter passing. It defines what a method is, how to declare a method by specifying the return type, name, and parameters. It explains how methods can call each other and are executed sequentially on the call stack. Finally, it discusses that Java passes arguments by value, not by reference, when passing parameters to methods.

Uploaded by

zineb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views12 pages

2.6 Method Calling: Application Programming: Java Programming

This document discusses Java methods, including defining methods, calling methods, and parameter passing. It defines what a method is, how to declare a method by specifying the return type, name, and parameters. It explains how methods can call each other and are executed sequentially on the call stack. Finally, it discusses that Java passes arguments by value, not by reference, when passing parameters to methods.

Uploaded by

zineb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2.

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

• Demonstrate how to properly declare a method.

• Demonstrate how methods call each other

• Demonstrate how methods are executed in call stack

• Demonstrate parameter passing

• Understand that Java passes arguments by value.

3
What is a Method?
• A method refers to a piece of code referring to behaviors
associated either with an object or its class

• A code found in a class for responding to a message

• The executable code that implements the logic of a


particular message for a class

• An operation or function that is associated with an object


and is allowed to manipulate the object's data

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

• Ways of calling a method


1. Calling a method through its object name
2. Calling a method within the same class
3. Calling a static method through its class name

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

invoke print() execute print()


print()
invoke compute() compute() execute compute()

invoke check() check() execute check()

main() begins main() main() ends

8
Passing Parameters
Passing parameters in Java is always Pass by Value!

When passing a parameter of primitive type:


• A copy of the value of the variable is passed
• The passed variable cannot be changed in the called method as
the method only possesses a copy of that variable.
When passing a parameter of reference type:
• A copy of the reference (address) of the object is passed
• The object reference cannot be changed in the called method
(i.e., the object cannot be reassigned to another object)
• The object state can be changed in the called method (i.e.,
attributes can be modified)

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

You might also like