Chapter 6: User-defined Methods
STATIC METHODS
Objectives
}
CMP 167
2
Define and use static methods
Define and use overloaded methods
Learn about variable scope
Learn parameters
Use return statements
Method
CMP 167
3
Method Definition/Declaration Syntax
CMP 167
4
Creating a Method
/*
CMP 167
5
Method Declaration (Refer to the previous example)
CMP 167
6
Modifiers
CMP 167
7
Method Calling and Method Calling Syntax
Method Call Syntax:
methodName(values for the parameters);
CMP 167
8
Example of Method Declaration and Method Call
CMP 167
9
Information to Remember when Writing Methods
CMP 167
10
A Tutorial Video on Methods (Play the Video)
CMP 167
11
Scope of variable/method definitions
TEACH A COURSE
12
�Method Parameters and arguments
TEACH A COURSE
13
Parameterized Method Example
CMP 167
14
Example: Writing a method
TEACH A COURSE
15
Example: Writing a method
* This method takes two parameters are prints the sum of the parameters.
* It returns no value
TEACH A COURSE
16
A Video Tutorial on Variable Scope (Play the Video)
CMP 167
17
Why Use Methods?
CMP 167
18
Return
TEACH A COURSE
19
Return Statement Syntax and Semantics
Return <expression>
A value we get from a return statement can be stored in a variables
TEACH A COURSE
20
Calling Methods in Expressions
TEACH A COURSE
21
Example: Writing a method that returns a value
TEACH A COURSE
22
Example:
public class MethodExample {
public static int getTotal(int num1, int num2) {
TEACH A COURSE
23
Calling methods from methods
TEACH A COURSE
24
Method Name Overloading
TEACH A COURSE
25
Method name overloading
TEACH A COURSE
26
�Parameter Error Checking�
TEACH A COURSE
27