Java Method Definition

Introduction

Java classes usually consist of two things: instance variables and methods.

This is the general form of a method:

type name(parameter-list ) {  
   // body of method  
} 

type specifies the type of data returned by the method.

Methods can have a return type or must be marked as void.

You can use the following form of the return statement:

return value; 

The following code adds a method to LegoBlock:

class LegoBlock {
  double width;/*from   w  w  w  . j  av a  2 s.c  om*/
  double height;
  double depth;

  // display volume
  void volume() {
    System.out.print("Volume is ");
    System.out.println(width * height * depth);
  }
}
  
public class Main {
  public static void main(String args[]) {
    LegoBlock myLegoBlock1 = new LegoBlock();
    LegoBlock myLegoBlock2 = new LegoBlock();

    // assign values to myLegoBlock1's instance variables
    myLegoBlock1.width = 10;
    myLegoBlock1.height = 20;
    myLegoBlock1.depth = 15;

    /* assign different values to myLegoBlock2's
       instance variables */
    myLegoBlock2.width = 3;
    myLegoBlock2.height = 6;
    myLegoBlock2.depth = 9;

    // display volume of first box
    myLegoBlock1.volume();

    // display volume of second box
    myLegoBlock2.volume();
  }
}

Returning a Value from method

The following code shows how to return a value from a method.

// volume() returns the volume of a box.

class LegoBlock {
  double width;//  w w w . java  2 s  . c  om
  double height;
  double depth;

  // compute and return volume
  double volume() {
    return width * height * depth;
  }
}
  
public class Main {
  public static void main(String args[]) {
    LegoBlock myLegoBlock1 = new LegoBlock();
    LegoBlock myLegoBlock2 = new LegoBlock();
    double vol;

    // assign values to myLegoBlock1's instance variables
    myLegoBlock1.width = 10;
    myLegoBlock1.height = 20;
    myLegoBlock1.depth = 15;

    /* assign different values to myLegoBlock2's
       instance variables */
    myLegoBlock2.width = 3;
    myLegoBlock2.height = 6;
    myLegoBlock2.depth = 9;

    // get volume of first box
    vol = myLegoBlock1.volume();
    System.out.println("Volume is " + vol);

    // get volume of second box
    vol = myLegoBlock2.volume();
    System.out.println("Volume is " + vol);
  }
}

The type of data returned must be compatible with the return type specified by the method.

The variable receiving the returned value must be compatible with the return type specified for the method.

Void method

A void method does not return a value.

The following code defines a method named printGrade and invokes it to print the grade for a given score.

public class Main {
  public static void main(String[] args) {
    System.out.print("The grade is ");
    printGrade(78.5);//from  w  w  w. j  a va2s.  c o  m

    System.out.print("The grade is ");
    printGrade(59.5);
  }

  public static void printGrade(double score) {
    if (score >= 90.0) {
      System.out.println('A');
    } 
    else if (score >= 80.0) {
      System.out.println('B');
    } 
    else if (score >= 70.0) {
      System.out.println('C');
    } 
    else if (score >= 60.0) {
      System.out.println('D');
    } 
    else {
      System.out.println('F');
    }
  }
}

value returned method

public class Main{
  public static void main(String[] args) {
    System.out.print("The grade is " + getGrade(78.5));
    System.out.print("\nThe grade is " + getGrade(59.5));
  }/*ww w  .j av a  2s.  c om*/

  public static char getGrade(double score) {
    if (score >= 90.0)
      return 'A';
    else if (score >= 80.0)
      return 'B';
    else if (score >= 70.0)
      return 'C';
    else if (score >= 60.0)
      return 'D';
    else
      return 'F';
  }
}



PreviousNext

Related