Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Methods: Methods: Making Your Application Modular. Making Your Application Modular.

Overview
In these chapters you will learn: What is a method? Declaring methods. Using (or calling) methods. Writing methods that take parameters. Writing methods that return values Declaring variables in the method.
Scope of variables.

Method overloading
2

What is a method? What is a method?

Java Methods
In Java, a method is a block of statements that has a name and can be executed by calling it from some other place in your program. You have already been using methods. For example:
to print text to the console, you use the println or print methods. to get an integer from the user, you use the nextInt method. and the granddaddy of all methods, main.

With the exception of main, all the methods youve used so far have been methods that are defined by the Java API and that belong to a particular Java class. The main method belongs to the class defined by your application.
4

Why Do We Need Methods?


Modular programming: breaking a program up into smaller, manageable modules or methods. Motivation for modular programming:
Simplifies the process of writing programs Enables a divide-and-conquer approach Improves maintainability of programs Prevents repeating code within the same program. Promotes code-reusability in later programs

Modular Design
Here is an example of non-modular design
public class MyClass { public static void main(String[] args) { statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement;

} }
7

Modular Design
This is an example of a modular design using methods
public class MyClass { public static void main(String[] args) { statement; statement; doTask1(); doTask2(); statement; statement; public static void doTask2( ) { statement; statement; statement; } public static void doTask1( ) { statement; statement; statement; statement; }

} }

The Basics of Making a Method


All methods including the main method must begin with a method declaration. Heres the basic form for a method declaration, at least for the types of methods we have seen so far:
public static return-type method-name( parameter-list ) { statements ... }
This method can be seen by other classes. Lists what can be passed to the method . The name of this method.

The data type of the value returned by the method when it ends

The body of the method

This method can exist even if no objects are created from this class. Will explain this more later.

Before We Get Deeper


To promote software reusability:
every method should be limited to performing a single, well-defined task. the name of the method should express that task effectively.

A small method that performs one task is easier to test and debug than a larger method that performs many tasks. If you cannot choose a concise name that expresses a methods task, your method might be attempting to perform too many diverse tasks. It is usually best to break such a method into several smaller method declarations.
10

Declaring Methods in Your Class


public class MyClass { public static void main(String[] args) { statement; statement; } //---------------------------------------------public static int doSomething( int value) { statement; statement; } //---------------------------------------------public static double dosomethingElse( double num1) { statement; statement; } }

12

Using Your Methods: Calling a Method Using Your Methods: Calling a Method

13

Calling a Method
To call (or execute) a method, use the method name followed by () and ;
printMsg(); // doesnt take parameters

When called, program executes the body of the called method. After the method terminates, execution resumes in the calling method (for example the main) at the point of call. The main can call any number of methods Methods can call other methods.
14

Example
public class HelloWorldMethod { public static void main(String[] args) { sayHello(); } //------------------------------------public static void sayHello() { System.out.println("Hello, World!"); } }

Execution starts at main

public class HelloWorldMethod { public static void sayHello() { System.out.println("Hello, World!"); } //------------------------------------public static void main(String[] args) { sayHello(); } }
15

Methods that Take Parameters Methods that Take Parameters

16

Methods that Accept Parameters


A parameter is a value that you can pass to a method.
The method can then use the parameter as if it were a local variable initialized with the value of the variable passed to it by the method call.

A method that accepts parameters must list the parameters in the method declaration. The parameters are listed in a parameter list thats in the parentheses that follow the method name. For each parameter used by the method, you list the parameter type followed by the parameter name. If you need more than one parameter, you separate them with commas.
17

You might also like