iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Methods

Methods are functions attached to a class. Signatures (name + parameter types) drive overloading and overriding; modifiers (static, final, abstract) drive how they’re called and overridden.

Methods, overloading, varargs, references

EXAMPLE
public class MethodExamples {

    // 1) Standard methods
    public int add(int a, int b)     { return a + b; }
    public double add(double a, double b) { return a + b; }   // overload by type
    public int add(int a, int b, int c)    { return a + b + c; }  // overload by arity

    // 2) Varargs — accept any number of ints
    public int sum(int... nums) {
        int s = 0;
        for (int n : nums) s += n;
        return s;
    }

    // sum() / sum(1) / sum(1,2,3,4,5) all work.

    // 3) Static methods — belong to the class, not an instance
    public static String escape(String s) {
        return s.replace("<", "&lt;").replace(">", "&gt;");
    }

    // 4) Method references — Class::method
    List<String> names = List.of("Ada", "Bo", "Cy");
    names.forEach(System.out::println);
    names.stream().map(String::toUpperCase).toList();

    // 5) Default methods on interfaces — give a sensible fallback
    interface Speaks {
        String name();
        default String greet() { return "Hi, I'm " + name(); }
    }

    record Robot(String name) implements Speaks {}
    System.out.println(new Robot("R2").greet());

    // 6) Override correctly — use the @Override annotation
    static class Animal { String sound() { return "?"; } }
    static class Dog extends Animal {
        @Override String sound() { return "woof"; }   // compiler catches typos
    }

    // 7) final methods — cannot be overridden
    public final int hashCodeFingerprint() { return 42; }

    // 8) Recursive method — Fibonacci
    public long fib(int n) {
        return n < 2 ? n : fib(n - 1) + fib(n - 2);
    }

    // 9) Throws — checked exceptions in the signature
    public String read(Path p) throws IOException {
        return Files.readString(p);
    }

    // 10) Generic method
    public static <T> T firstNonNull(T a, T b) {
        return a != null ? a : b;
    }

    public static void main(String[] args) {
        MethodExamples m = new MethodExamples();
        System.out.println(m.add(2, 3));
        System.out.println(m.sum(1, 2, 3, 4, 5));
        System.out.println(MethodExamples.escape("<b>"));
        System.out.println(firstNonNull(null, "fallback"));
    }
}

Why it matters

@Override is free seatbelt — it’s a compile-time check that you’re actually overriding (not just adding a new method with a typo'd name). Make it a lint rule.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }  // overload
Try it Yourself »

Exercise

Return the sum of two ints.

static int add(int a, int b) { a + b; }

Discussion

Loading…