Methods
Methods
Complete lesson for Methods using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Methods
Section: Methods. Java methods (not functions): signature, overloading, varargs, pass-by-value, and return types.
Methods, not functions
- Java has **methods** — behavior that belongs to a class or instance. There are no free-standing functions like in C or Python.
- A method declaration: modifiers, return type, name, parameter list, optional
throws, then a body (or;ifabstract/native). voidmeans no return value.return;exits early from a void method.
public class MethodBasics {
static int add(int a, int b) {
return a + b;
}
static void greet(String name) {
System.out.println("Hi, " + name);
}
public static void main(String[] args) {
greet("Ada");
System.out.println(add(2, 3));
}
} Overloading vs overriding (preview)
- Overloading: same method name, different parameter lists, resolved at **compile time**. Return type alone cannot distinguish overloads.
- Overriding: same signature in a subclass, resolved at **runtime** — covered fully under polymorphism.
| Idea | Overloading | Overriding |
|---|---|---|
| Where | Same class (or inherited name) | Subclass / implementing class |
| Parameters | Must differ | Must match (same signature) |
| When chosen | Compile time | Runtime (virtual dispatch) |
| Keywords | None special | @Override, cannot be static |
Varargs and pass-by-value
void log(String... parts)is an array parameter. It must be the last parameter. A calllog()passes a zero-length array, notnull.- Java is **pass-by-value**. For objects, the value is the reference: the method can mutate the object, but
obj = somethingElsedoes not change the caller’s variable.
Advanced — Methods
Signatures, hiding, and main
- The JVM signature is name + parameter types (type erasure applies to generics).
throwsis not part of the overload key. - A static method with the same signature in a subclass *hides* the parent method; it does not override.
maincan be overloaded, but the launcher only callspublic static void main(String[]).- Avoid too many overloads that differ only by
intvsIntegeror array vs varargs — the compiler’s most-specific rule surprises people.
Tricky Interview Points
Q1) Does Java have functions?
Answer Not as top-level functions. You write methods on classes (or static methods). Lambdas are instances of functional interfaces, not free functions.
Q2) What is pass-by-value in Java?
Answer Arguments are copied. For primitives the bits are copied. For objects the reference is copied, so the callee can change the object state but not the caller’s reference variable.
Q3) Can you overload by return type only?
Answer No. The parameter list must differ. Return type is not part of the overload identity.
Q4) What are varargs?
Answer A variable-arity parameter written Type... name. The compiler packs arguments into an array. Only one varargs, and it must be last.
Q5) static method vs instance method?
Answer static belongs to the class and cannot use this or instance fields. Instance methods need an object (unless called from another instance method of the same object).
Q6) Is main a special keyword?
Answer No. It is a method the launcher looks up by signature. You can have other methods named main.
Simple Programs
Overload and varargs
public class OverloadDemo {
static int max(int a, int b) {
return a > b ? a : b;
}
static int max(int a, int b, int c) {
return max(max(a, b), c);
}
static int sum(int... nums) {
int s = 0;
for (int n : nums) s += n;
return s;
}
public static void main(String[] args) {
System.out.println(max(4, 9));
System.out.println(max(4, 9, 1));
System.out.println(sum(1, 2, 3, 4));
}
} Problem Solving Tricks
- Name methods as verbs:
calculateTax, nottax. - Keep parameter lists short; wrap many options in an object or a builder.
- Do not overload
staticand instance methods with the same name unless you enjoy confusing readers. - Document units (
millis,indexInclusive) in the method name or Javadoc.
Practice Programs List
- Write overloaded
areamethods for circle (radius) and rectangle (width, height). - Write
average(double... values)that throws if length is 0. - Show pass-by-value: a method that does
x = 100on anintand one that doeslist.addon anArrayList. - Overload
printforint,String, andint[]. - Split a “god method” that reads, computes, and prints into three methods.