Data Structure & Algorithm in Java

  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Recursion
  5. Method Calls and Recursion Implementation

Method Calls and Recursion Implementation


In Java, a method call is an operation that invokes a particular method with specified arguments. When a method is called, the Java Virtual Machine (JVM) executes the method’s code.

Method calls can be divided into two categories:

types of method calls
  • Static Method Calls: Static methods belong to the class rather than any instance of the class. They can be called using the class name followed by the dot operator. For example:
  • Instance Method Calls: Instance methods are associated with objects of the class. They are invoked using an object reference followed by the dot operator. For Example:

Recursion is a programming technique where a function calls itself to solve a problem. Recursive functions have two main components: the base case(s) and the recursive case(s).

  • Base Case: The base case(s) provide termination conditions for the recursion. When the base case is met, the recursion stops, and the function returns a specific value without making further recursive calls. Without base cases, recursion can lead to an infinite loop.
  • Recursive Case: The recursive case(s) define how the function calls itself with modified arguments to solve smaller subproblems. Recursive calls continue until the base case is reached. Each recursive call typically reduces the problem size or complexity, bringing it closer to the base case.

Example: Factorial Calculation Using Recursion

Let’s illustrate recursion with an example of calculating the factorial of a number:

ublic class FactorialCalculator {
    // Recursive function to calculate the factorial of a number
    public static int factorial(int n) {
        // Base case: if n is 0 or 1, factorial is 1
        if (n == 0 || n == 1) {
            return 1;
        } else {
            // Recursive case: factorial of n is n multiplied by factorial of (n-1)
            return n * factorial(n - 1);

    public static void main(String[] args) {
        int number = 5; // Change this number to calculate factorial for a different value
        int result = factorial(number);
        System.out.println("Factorial of " + number + " is: " + result);


Factorial of 5 is: 120

How can we help?

Leave a Reply

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