Skip to content

Methods

Büşra Oğuzoğlu edited this page Jul 6, 2022 · 13 revisions

A method in Java is a collection of statements that are grouped together to perform an operation, it has inputs (can be multiple) and a single output (the return value).

Structure of a method is as follows:

private static double findAverage(double[] numbers) {
    // Method Body
    double sum = 0.0;
    for (double e : numbers)
        sum += e;
    return sum / numbers.length;
}
  • private static double findAverage(double[] numbers) is the method header.

  • double is the return type.

  • findAverage is the method name.

  • double[] numbers are the method parameters.

  • Methods may have a return statement, they do not need to have it if their return type is void.

Clone this wiki locally