Java Methods Part1
Java Methods Part1
Java Methods Part1
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
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
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 data type of the value returned by the method when it ends
This method can exist even if no objects are created from this class. Will explain this more later.
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
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!"); } }
public class HelloWorldMethod { public static void sayHello() { System.out.println("Hello, World!"); } //------------------------------------public static void main(String[] args) { sayHello(); } }
15
16
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