Definition and declaration

Method Declaration

What is a Method Declaration?

A method declaration in Java specifies the method’s signature, including its name, return type, and parameters. It provides the compiler with information about how the method can be called but does not include the actual implementation of the method’s functionality.

Components of a Method Declaration

  1. Access Modifier:
    Specifies the visibility of the method (e.g., public, private, protected).
  2. Return Type:
    Indicates the type of value the method returns (e.g., void, int, String).
  3. Method Name:
    The name of the method, following Java naming conventions.
  4. Parameter List:
    A comma-separated list of input parameters, each with a type and a variable name, enclosed in parentheses.
  5. Exceptions (Optional):
    Specifies any exceptions that the method might throw.

Example

public int addNumbers(int a, int b);
  • public: Access modifier.
  • int: Return type.
  • addNumbers: Method name.
  • (int a, int b): Parameter list.

Method Definition

What is a Method Definition?

A method definition in Java includes the method declaration and the body. The body contains the actual code that defines what the method does, including any computations, logic, and return statements.

Components of a Method Definition

  1. Method Declaration:
    Includes the access modifier, return type, method name, and parameter list.
  2. Method Body:
    Enclosed in curly braces {}, containing the statements that define the method’s behavior.

Example

public int addNumbers(int a, int b) {
    int sum = a + b;
    return sum;
}

Detailed Breakdown of the Example

Method Declaration:

public int addNumbers(int a, int b)
  • public: The method is accessible from other classes.
  • int: The method returns an integer value.
  • addNumbers: The name of the method.
  • (int a, int b): Two parameters of type int are passed to the method.

Method Body:

{
    int sum = a + b;
    return sum;
}
  • int sum = a + b;: The method adds the two parameters a and b, storing the result in the variable sum.
  • return sum;: The method returns the value of sum.

Combining Declaration and Definition

In Java, method declaration and definition are typically combined in a single code block within a class. Here’s a complete example with comments to illustrate both aspects:

public class Calculator {

    // Method Declaration and Definition
    public int addNumbers(int a, int b) {
        // Method Body
        int sum = a + b;  // Calculate sum
        return sum;       // Return sum
    }
    
    // Another Method Declaration and Definition
    public void printSum(int a, int b) {
        int sum = addNumbers(a, b);  // Call addNumbers method
        System.out.println("Sum: " + sum);  // Print the sum
    }
    
    // Main method to execute the program
    public static void main(String[] args) {
        Calculator calc = new Calculator();  // Create an instance of Calculator
        calc.printSum(5, 10);  // Call printSum method
    }
}

Summary

  • Method Declaration:
    Specifies the method’s name, return type, and parameters. It tells what the method is supposed to do.
  • Method Definition:
    Includes the declaration and the body, which contains the actual code implementing the method’s functionality.