1 of 27

Chapter 6: User-defined Methods

STATIC METHODS

2 of 27

Objectives

  • public static int max(int a, int b){
  • if (a > b) {
  • return a;
  • }
  • else {
  • return b;
  • }

}

CMP 167

2

Define and use static methods

Define and use overloaded methods

Learn about variable scope

Learn parameters

Use return statements

3 of 27

Method

  • A method is a named block of code which only runs when it is called.
  • Methods are used to perform certain actions, and they are also known as functions.
  • A method must be defined within a class.
  • method definition consists of the new method's name and a block of statements(method body)
  • Ex: public static void display(){ // method body }
  • method call is an invocation of a method's name, causing the method's body statements to execute.
  • A method only executes when it is called

CMP 167

3

4 of 27

Method Definition/Declaration Syntax

  • modifiers returnType methodName(Parameters List){
  • method body
  • }

CMP 167

4

5 of 27

Creating a Method

  • public class Methods {

/*

  • * Methods are created within a class.
  • * Look at the example below.
  • */
  • public static void methodName() {
  • /*
  • * Method body goes here. This is a list of
  • * statements that you would like your method to do.
  • */
  • }
  • }

CMP 167

5

6 of 27

Method Declaration (Refer to the previous example)

  • public static: These are the modifiers in the method
  • void: This is the return type for the method
  • methodName: This is the Method Name
  • Since the () are empty: There is no parameters in this method
  • {}:  Everything enclosed between braces is part of the method body

CMP 167

6

7 of 27

Modifiers

  • Modifiers define where the method can be accessed from in an application.
  • 4 access modifiers available in Java
  • public: accessible in all classes in your application.
  • protected: accessible within the class in which it is defined and in its subclass(es)
  • private: accessible only within the class in which it is defined.
  • default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.

CMP 167

7

8 of 27

Method Calling and Method Calling Syntax

  • A method does not run until it is called
  • A method can called multiple times

Method Call Syntax:

methodName(values for the parameters);

CMP 167

8

9 of 27

Example of Method Declaration and Method Call

  • public class MethodExample {
  • /*
  • * Recall that methods are created with a class.
  • * The following method prints "My First Method" when called.
  • * It takes no parameters and returns no value.
  • */
  • public static void display() {
  • System.out.println("My First Method");
  • }

  • public static void main(String[] args) {
  • display(); // Method call
  • }
  • }

CMP 167

9

10 of 27

Information to Remember when Writing Methods

  • What is the name of your method? Choose a name that describes what the method does
  • Does your method take any parameters?
    • If you answer is no, leave the parenthesis black.
    • If yes, follow the following steps to create your parameters
      • How many parameters does the method take?
      • What are the data types of the parameters
      • What are the names of the parameters
  • What is the returnType of your method.
    • If your method returns no value, the return type is void
    • If the method return a value, we put the data type of the value that we expect to be returned by the method

CMP 167

10

11 of 27

A Tutorial Video on Methods (Play the Video)

CMP 167

11

12 of 27

Scope of variable/method definitions

  • The name of a defined variable or method item is only visible to part of a program, known as the item's scope
  • A variable declared in a method has the scope limited to that method.
  • A variable declared in a method cannot be accessed and used outside of the method
  • We can be able to declare variables outside of any method but within a class.
  • A variable declared within a class but outside any method is called a class member variable or field.
  • A variable declare anywhere within a method is referred to as a local variable
  • We will discuss class variables in chapter 8

TEACH A COURSE

12

13 of 27

Method Parameters and arguments

  • parameter is a method input specified in a method definition.
  • Parameters act as variables inside the method.
  • Parameters are placed after the method name, inside the parentheses.
  • Parameters provide a way of getting information from outside the method to be used in the method body
  • A method can have no parameters
  • You can also be able to define multiple parameter separated by commas
  • An argument is a value provided to a method's parameter during a method call

TEACH A COURSE

13

14 of 27

Parameterized Method Example

  • public class MethodExample {
  • /*
  • * When called, this method prints an integer value stored in a parameter.
  • * It takes one integer parameter and returns no value
  • */
  • public static void displayNum(int num) {
  • System.out.println("The number is " + num);
  • }

  • public static void main(String[] args) {
  • displayNum(20); // Method call
  • displayNum(30); // Method call
  • }
  • }

CMP 167

14

15 of 27

Example: Writing a method

  • Write a program with a method named printTotal that accepts two integers as an argument and prints the sum of the two integers. Call this method from main(). (See solution in the next Slide)

TEACH A COURSE

15

16 of 27

Example: Writing a method

  • public class MethodExample {
  • /*

* This method takes two parameters are prints the sum of the parameters.

* It returns no value

  • */
  • public static void printTotal(int num1, int num2) {
  • int total = num1 + num2;
  • System.out.println(total);
  • }
  • public static void main(String[] args) {
  • printTotal(20, 30); // prints 50
  • printTotal(60, 60); // prints 120
  • }
  • }

TEACH A COURSE

16

17 of 27

A Video Tutorial on Variable Scope (Play the Video)

CMP 167

17

18 of 27

Why Use Methods?

  • To reuse code: define the code once and use it many times.
  • Decomposing a program into methods can greatly aid program readability, helping yield an initially correct program, and easing future maintenance.
  • Avoid writing redundant code

CMP 167

18

19 of 27

Return

  • If you remember, we said parameters provide a way of getting values into a method.
  • We can also be able to get a value out from a method using the return statement
  • To return a value from a method we use the keyword return.
  • Return is used to exit a method with or without a value from a method
  • The data value of the values that gets returned must match returnType of a method
  • A method can only return one item, not two or more.
  • A method that does not return any value has void as the return type

TEACH A COURSE

19

20 of 27

Return Statement Syntax and Semantics

  • Syntax:

Return <expression>

A value we get from a return statement can be stored in a variables

  • Semantics
  • Evaluate the <expression>
  • Take the result from step 1 to wherever the method was called from and exit from the method

TEACH A COURSE

20

21 of 27

Calling Methods in Expressions

  • A method call evaluates to the returned value. Therefore a method call can appear within an expression or can printed
  • A method with a void return type cannot be used within an expression

TEACH A COURSE

21

22 of 27

Example: Writing a method that returns a value

  • Write a program with a method named getTotal that accepts two integers as an argument and returns the sum of the two integers. Call this method from main().

TEACH A COURSE

22

23 of 27

Example:

public class MethodExample {

  • /*
  • * This method takes two parameters are return the sum of the parameters.
  • */

public static int getTotal(int num1, int num2) {

  • int total = num1 + num2;
  • return total;
  • }
  • public static void main(String[] args) {
  • System.out.println( getTotal(20, 30)) ; // prints 50, the value returned from the method call
  • int sum = getTotal(60, 60); //The method call evaluates 120, which is kept in variable sum
  • System.out.println(sum); // prints 120, the value in the variable sum
  • }
  • }

TEACH A COURSE

23

24 of 27

Calling methods from methods

TEACH A COURSE

24

25 of 27

 Method Name Overloading

  • Method name overloading or just method overloading is a situation where two or more methods in the same class have the same name but differing in the number or types of parameters
  • We determine which method to call based on the argument types
  • We allowed to define more than two same-named methods as long as each has distinct parameter types
  • A method's return type does not influence overloading

TEACH A COURSE

25

26 of 27

 Method name overloading

  • method name overloading or just method overloading is a situation where two or more methods in the same class have the same name but differing in the number or types of parameters

TEACH A COURSE

26

27 of 27

 �Parameter Error Checking

  • Method name overloading or just method overloading is a situation where two or more methods in the same class have the same name but differing in the number or types of parameters

TEACH A COURSE

27