Methods
A method is a named block of code you call with arguments. It may return a value. In Java every method belongs to a class or interface.
Detailed Explanation
Methods are how you name a piece of work and reuse it. withdraw(int paise) hides the arithmetic and the overdraft check. Callers read the name and the parameter list; they should not need the body.
A method declaration has modifiers, a return type (void if it returns nothing), a name, parameters, an optional throws clause, and a body. Abstract methods and interface methods may omit the body. The signature that matters for overloading is the name plus the parameter types. The return type is not part of that signature.
Instance methods run on an object. Inside the body, this is that object, and you can read its fields. Static methods run on the class. They have no this and cannot touch instance fields without an explicit object. Call instance methods as account.withdraw(50) and static methods as Math.max(a, b).
Arguments are passed by value. For primitives, the method gets a copy of the number. For references, the method gets a copy of the reference, so it can mutate the same object but cannot rebind the caller's variable. swap(a, b) that assigns the parameters does not swap the caller's variables.
Overloading lets several methods share a name with different parameter lists. Overriding replaces a parent instance method. Those are different mechanisms: overload is chosen at compile time, override at runtime. Recursion is a method calling itself; it needs a base case or the stack overflows.
You reach for a new method when a block has a name, when it is used twice, or when a method has grown past one screen. Names should be verbs or verb phrases (isEmpty, toUtc, parse). A method that both changes state and returns a computed value is harder to use; pick one job when you can.
The cost of tiny methods is navigation. The cost of huge methods is untestable logic. Prefer methods that fit in your head. Keep parameter lists short; past three or four, a small object or a record is clearer. Document side effects in the name (save, send) so callers are not surprised.
Code Examples
public class Tax {
static int withGst(int paise) { // static: no object needed
return paise + paise * 18 / 100;
}
public static void main(String[] args) {
System.out.println(withGst(10000)); // prints: 11800
}
}public class Counter {
private int n;
public void bump(int by) { // instance: uses this.n
this.n += by;
}
public int get() {
return n;
}
public static void main(String[] args) {
Counter c = new Counter();
c.bump(3);
System.out.println(c.get()); // prints: 3
}
}Common Gotchas
Reassigning a parameter does not change the caller
`ref = new Thing()` inside the method leaves the caller's variable pointing at the old object.
A missing `return` on a non-void method does not compile
every path must return or throw.
`main` must be `public static void main(String[] args)`
a different signature is a valid method the JVM will not use as an entry point.
Too many overloads hide the wrong call
prefer distinct names when the jobs differ.