JAVA Methods

A METHOD is a named set of instructions or statements which performs a specific task
A method is complete sub program in itself
A method is defined once in a program and can be invoked or called anywhere  from main program.
Each method in program has its own unique name
A method is invoked and called using its name in the main program.
when the name of method is encountered in a program (i.e. when method is invoked or called), the execution of program is branches to the body of the method. And when method is finished the execution of the program returns to the area of the program code from where it was called, and the program continues to the next line of code.

Dividing of the program into small sub programs (methods) is called modularization. A modular program is well structured.

A method is usually created to avoid the repetition of the same code in the program.

In order to better understand the concept of method please look the below program in screen shot and its output


In the above program we created a method named as subprogram() supposed to print the message Welcom to JAVA World on console.

static void subprogram() {
               System.out.println("Welcome to JAVA World");
                return;

Then this method is called three times from the main program as
subprogram();
               subprogram();
                subprogram();
and it prints three times the same message on screen.
Welcome to JAVA World
Welcome to JAVA World
Welcome to JAVA World



Method Structure or Syntax of the method

Below is the general syntax for declaring a class method.


access-modifier optional return-type method-name (optional Parameter list)

{

optional exception-handling code block

}

Here is an example of a typical method declaration:

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.

Method Name with optional parameter list is called signature of the method
e.g.

calculateAnswer(double wingSpan, int numberOfEngines,double length, double grossTons)


In short method declarations have six components, in order:
  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. An exception list—to be discussed later.
  6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

Class name, method name and name of variable should be meaningful