Method In Java With Example

Methods are truly the heart and soul of the java programs. A method is a self contained block of code that performs a specific task. They provide a way of defining the behavior of an object i.e. what the object does. Methods break up large and complex calculations in program that might involve many lines of code into more manageable chunks. All the execution that takes place in any application is within a method.


Table of Contents

Syntax of Method:

Following is the syntax of method which consists of method header and method body:

returnType methodName(Parameter list)  //Method Header

{

//declarations and statements

//Method Body

}

Parts of Method Definition

Method definition consists of two parts:

  • Method header
  • Method body 

Method Header: A method header consists of method’s return type followed by the method name and optional parameter list enclosed in the parenthesis.

  • The returnType in a method header specifies the type of value if any, that the method returns. If the method does not return a value then the return type must be void. For example: return type may be void, int etc.
  • The methodName in the method header specifies the name of the method. It follows the same rules and conventions as that of identifiers. For example: method name may be area, display, show etc.
  • The parameter list in the method header must be enclosed in parenthesis. A parameter is a variable that temporarily stores data values known as arguments that are being passed to the method whenever it is called. If no arguments are being passed to the method then the parenthesis remains empty. The parameter list takes the following form: (datatype varName1, datatype varName2, …….)
  • Examples of method header is:
int area(int l, int b)

void show();

Important Note: The parameters specified in the method header are also known as formal parameters and the arguments that are passed to the method are also known as actual parameters.

Method Body: The method’s body enclosed in a pair of curly braces consists of local variables and constant declarations for use within the method and sequence of executable statements that takes some kind of action when the method is invoked.


Program Example of Method

Let us take an example to show the method definition in the class.

Step 1: First we create a class MethodDemo in which we define methods:

class MethodDemo

{

  int length,breadth;

  void getData(int l,int b) //method definition

  {

    length=l;

    breadth=b;

  }

  int area() //method definition

  {

    int rectArea=length*breadth;

    return rectArea;

  }

}

Step 2: Second we create a class Rectangle in which we call the methods of above class:

class Rectangle

{

  public static void main(String[] args)

  {

    MethodDemo obj=new MethodDemo();

    obj.getData(6,9);

    int result=obj.area();

    System.out.println("Area of rectangle is:"+result);

  }

}

Output:

Area of rectangle is: 54

Returning a Value From Method

To return a value from a method, the return statement is used. Its syntax is as follows:

return(expr);

Here expr is a variable, constant value or an expression.

On execution of the return statement, the program control immediately returns to the point where the method was called. If the expression is the part of the return statement then the value of that expression is returned as the value of the method invocation.

For Example: Let us take a small segment from the program example of method in which we have used return statement:

int area()

{

  int rectArea=length*breadth;

  return rectArea;

}

Here this method area calculates the area of rectangle whose length and breadth is given and returns the area as an integer.

Since we know that void does not return any value then you can omit the return statement or just use the keyword return by itself to the end of the execution of method like this:

return;

Important Note: There can be more than one return statement in a method. For example:

int maximumVal(int a, int b)

{

  if(a>b)

  {

    return a;

  }

  else

  return b;
}

Leave a Reply

Your email address will not be published. Required fields are marked *