Tutorials Logic, IN info@tutorialslogic.com

Methods in Java Declaration, Overloading, Recursion

Method Declaration and Return Values

Java methods package behavior behind typed parameters and return values. Their contracts should make mutation, failure, overloading, static access, and recursion boundaries clear.

A Java method contract defines accepted arguments, returned results, mutation, failure, and whether dispatch is static, overloaded, recursive, or instance-based.

A method declaration includes access modifier, optional static, return type, method name, parameters, and body.

Simple Methods

Simple Methods
public class MethodBasics {
    static int add(int a, int b) {
        return a + b;
    }

    static void printLine(String text) {
        System.out.println(text);
    }

    public static void main(String[] args) {
        int total = add(10, 20);
        printLine("Total: " + total);
    }
}

Parameters and Pass-by-Value

Java passes arguments by value. For primitives, the copied value is passed. For objects, the copied reference is passed.

Pass-by-Value

Pass-by-Value
public class PassByValueDemo {
    static void changeNumber(int n) {
        n = 99;
    }

    public static void main(String[] args) {
        int value = 10;
        changeNumber(value);
        System.out.println(value); // still 10
    }
}

Method Overloading

Overloading means multiple methods have the same name but different parameter lists. It improves readability when operations are conceptually the same.

Overloaded Methods

Overloaded Methods
public class OverloadingDemo {
    static int area(int side) {
        return side * side;
    }

    static int area(int length, int width) {
        return length * width;
    }

    public static void main(String[] args) {
        System.out.println(area(5));
        System.out.println(area(4, 6));
    }
}

Recursion

A recursive method calls itself. Every recursive method needs a base case to stop and a recursive step that moves toward the base case.

Factorial Recursion

Factorial Recursion
public class RecursionDemo {
    static int factorial(int n) {
        if (n <= 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        System.out.println(factorial(5));
    }
}

Writing Clear Methods

A good method has one clear job, meaningful parameters, and a predictable return value. If a method becomes hard to name, it may be doing too many things.

  • Use verbs in method names.
  • Keep parameter lists short.
  • Return a value instead of printing when logic should be reusable.
  • Document recursion base cases clearly.

Writing methods that do one clear job

A Java method is a named block of behavior. It may receive parameters, return a value, update object state, or perform an action. Good methods make code readable because the method name explains the intention and the body contains the steps. A method named calculateTax is easier to trust than repeating the same formula in five places.

Method signatures matter: the name, parameter list, and return type tell callers how to use the method. Overloading lets the same method name support different parameter lists, but it should still represent the same idea. Recursion is useful for problems that naturally shrink into smaller versions, but every recursive method needs a clear base case.

  • Use parameters for values the method needs from outside.
  • Return a value when the caller needs the result.
  • Avoid methods that secretly do many unrelated jobs.
  • Overload only when the method name still means the same action.

Method with parameters and a return value

Method with parameters and a return value
static double calculateFinalPrice(double price, double taxRate) {
    return price + (price * taxRate);
}

double amount = calculateFinalPrice(500.0, 0.18);
System.out.println(amount);
Before you move on

Methods in Java Declaration, Overloading, Recursion Mastery Check

4 checks
  • For primitives, the copied value is passed.
  • For objects, the copied reference is passed.
  • Overloading means multiple methods have the same name but different parameter lists.
  • It improves readability when operations are conceptually the same.

Method Contract Boundary

  • Ambiguous overloads

    Overloads that differ only by closely related numeric or reference types can be ambiguous with literals or null. Prefer names that communicate different behavior.

Core Java Questions Learners Ask

Java passes every argument by value. For an object, that value is a copy of the reference.

No. Overloading requires a different parameter list.

Use it when the operation does not depend on instance state.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.