Lesson 6: Methods, Overloading & Varargs

07 Jul 2026 1 min Swarnil Singhai

Methods package behavior; overloading lets the same name handle different argument shapes, and varargs handle 'however many' arguments.

Real-time example

A notification service exposes send(String to, String msg) and an overload send(List recipients, String msg) for broadcast.

void send(String to, String msg) {
    System.out.println("-> " + to + ": " + msg);
}
void send(String msg, String... recipients) {
    for (String r : recipients) send(r, msg);
}
// send("Server down", "ops@co.com", "oncall@co.com");

What's happening

The compiler resolves overloads at compile time by parameter types — this is 'static polymorphism', distinct from runtime polymorphism.

Varargs are just sugar for an array parameter — recipients is a normal String[] inside the method body.

Advertisement