Skip to main content

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.

L1

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 ; if abstract / native).
  • void means 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.
IdeaOverloadingOverriding
WhereSame class (or inherited name)Subclass / implementing class
ParametersMust differMust match (same signature)
When chosenCompile timeRuntime (virtual dispatch)
KeywordsNone 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 call log() passes a zero-length array, not null.
  • Java is **pass-by-value**. For objects, the value is the reference: the method can mutate the object, but obj = somethingElse does not change the caller’s variable.
L2

Advanced — Methods

Signatures, hiding, and main

  • The JVM signature is name + parameter types (type erasure applies to generics). throws is not part of the overload key.
  • A static method with the same signature in a subclass *hides* the parent method; it does not override.
  • main can be overloaded, but the launcher only calls public static void main(String[]).
  • Avoid too many overloads that differ only by int vs Integer or array vs varargs — the compiler’s most-specific rule surprises people.
L3

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.

L4

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));
    }
}
L5

Problem Solving Tricks

  • Name methods as verbs: calculateTax, not tax.
  • Keep parameter lists short; wrap many options in an object or a builder.
  • Do not overload static and instance methods with the same name unless you enjoy confusing readers.
  • Document units (millis, indexInclusive) in the method name or Javadoc.
L6

Practice Programs List

  1. Write overloaded area methods for circle (radius) and rectangle (width, height).
  2. Write average(double... values) that throws if length is 0.
  3. Show pass-by-value: a method that does x = 100 on an int and one that does list.add on an ArrayList.
  4. Overload print for int, String, and int[].
  5. Split a “god method” that reads, computes, and prints into three methods.