Skillversitas

Methods

A method in Java is a reusable block of code defined inside a class. Every method has a unique signature (name + parameters), a return type, and can have access modifiers (public, private, protected). Unlike Python, which uses def, Java uses more formal syntax.

Method overloading allows multiple methods with the same name but different parameters within one class. Varargs (Type... name) allows a variable number of parameters. Java 8+ supports lambda expressions for functional interfaces — a compact way to create anonymous functions.

Methods in Java can be static (belong to the class, no object required) or instance (belong to the object). Private methods (Java 9+) can only be called from within the same class, helping reduce duplication of internal code.

Method Components:

  • Access modifiers: public, private, protected, default (package)
  • Non-access modifiers: static, final, abstract, synchronized
  • Return type: void if it returns no value
  • Parameter list: (Type name, Type name)
  • Varargs: Type... name for a variable number of parameters

Code Example

public class JavaMethods {

    // Static method
    static int add(int a, int b) {
        return a + b;
    }

    // Method overloading
    static double add(double a, double b) {
        return a + b;
    }

    // Varargs
    static int total(int... numbers) {
        int sum = 0;
        for (int a : numbers) sum += a;
        return sum;
    }

    // Method with a default parameter (via overloading)
    static String greet(String name) {
        return greet(name, "Hello");
    }

    static String greet(String name, String greeting) {
        return greeting + ", " + name + "!";
    }

    // Lambda (Java 8+)
    interface Function {
        int calculate(int x);
    }

    public static void main(String[] args) {
        System.out.println(add(5, 3));       // 8
        System.out.println(add(2.5, 3.5));   // 6.0
        System.out.println(total(1, 2, 3, 4));  // 10
        System.out.println(greet("John"));        // Hello, John!
        System.out.println(greet("Anna", "Good morning")); // Good morning, Anna!

        // Lambda
        Function square = x -> x * x;
        System.out.println("Square of 5: " + square.calculate(5));
    }
}

Code Explanation

  1. Static method: static int add(int a, int b) — can be called without creating an object, just JavaMethods.add(5, 3).
  2. Method overloading: two methods named add — one for int, one for double. Java picks the appropriate one based on the argument types at the call site.
  3. Varargs: int... numbers accepts zero or more int arguments, which are packaged into an int[] array.
  4. Default parameter pattern: because Java does not support default parameters directly, use overloading — the method without the second parameter calls the method with the default value.
  5. Lambda: x -> x * x is a lambda expression for the functional interface Function. More compact than an anonymous class.

Everyday Example

A calculator program with various operations using overloading:

public class Kalkulator {
    static double calculate(String operation, double... numbers) {
        return switch (operation) {
            case "add" -> {
                double sum = 0;
                for (double a : numbers) sum += a;
                yield sum;
            }
            case "multiply" -> {
                double result = 1;
                for (double a : numbers) result *= a;
                yield result;
            }
            case "average" -> {
                double sum = 0;
                for (double a : numbers) sum += a;
                yield sum / numbers.length;
            }
            default -> throw new IllegalArgumentException("Unknown operation");
        };
    }

    public static void main(String[] args) {
        System.out.println("Add: " + calculate("add", 10, 20, 30));
        System.out.println("Multiply: " + calculate("multiply", 2, 3, 4));
        System.out.println("Average: " + calculate("average", 80, 90, 85, 70));
    }
}

Summary

Methods are the basic unit of execution in Java. Overloading provides flexibility without changing method names. Lambda expressions (Java 8+) bring concise functional programming. Always state access modifiers explicitly — do not rely on default package access.