Java Offer Related Method Signatures
Overloading gives the same method name to different parameter lists. Choose it when the operations share one clear meaning, not merely to avoid inventing a second name.
Understand the idea
The compiler resolves an overloaded call using the arguments’ compile-time types and applicable conversion rules. The return type alone cannot distinguish two overloads. Overloading is different from overriding, where an object supplies a subtype implementation of an inherited instance method. Be cautious with overloads accepting unrelated reference types: a null argument can make the intended choice ambiguous.
Run it locally
Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.
javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out MainA worked example
public class Main {
static String label(int seats) { return seats + " seats"; }
static String label(String session, int seats) { return session + ": " + seats + " seats"; }
public static void main(String[] args) {
System.out.println(label(3));
System.out.println(label("Mosaic", 3));
}
}
Expected output
3 seats
Mosaic: 3 seatsThe one-argument call selects the first declaration. The two-argument call selects the second without a runtime search through method bodies. Both methods describe seat labels, so the shared name is meaningful. If two variants represented unrelated actions, separate names would make call sites easier to review.
Check the official Java documentation for detailed contracts and supported options.