A method is a named, invocable block of code belonging to a class (or interface). It is the unit of reuse, testing, and API surface. In production Java, methods are how you hide representation and publish behaviour.
Mental model: the method header is the contract; the body is replaceable. Callers should depend on the header and documented exceptions, not on incidental side effects.
list.add; speculative “utilities” with no caller; breaking a transaction across two public methods that must always run together.A definition is a header plus a body. Interfaces and abstract methods may omit the body (or use default / static bodies).
[annotations]
[access] [static] [final] [synchronized] [native]
returnType name(parameterList) [throws CheckedException] {
// body
return value; // required unless return type is void
}
public, protected, package-private (no modifier), private. Prefer the tightest that still serves the API.this.void. Changing only the return type is not overloading.save), noun/adjective for queries (isEmpty, size).Main is a method: public static void main(String[] args) is an ordinary static method the launcher looks up by signature. You can overload main; the JVM starts the String[] form. Keep main thin and put real work in methods you can test.
Method signature (JLS): the name plus the parameter types (type erasure applies; see Generics). Parameter names are not part of the signature. Return type is not part of the signature.
Method prototype / header: the source line callers read — modifiers, return type, name, parameters, throws. School texts call this the prototype.
Return: exits the method and (if non-void) yields a value assignment-compatible with the declared type. A non-void method must return on every path, or throw. After return, no further statements in that path run.
Accessor vs mutator: an accessor (query / “pure” in school language) does not change observable instance state. A mutator (command / “impure”) does. Prefer queries that are referentially transparent when you can; document side effects when you cannot.
// Same signature — will not compile together
int compute(int a) { return a; }
void compute(int a) { } // error: return type is not in the signature
| Feature | Static | Instance |
|---|---|---|
| Keyword | static | none |
| Receiver | Class (or an instance, which is ignored and confusing) | A live object; NPE if the reference is null |
| this / super | Illegal | Available |
| Can touch | Static members directly; instance members only via an explicit reference | Instance and static members |
| Overriding | No. Hiding if a subclass declares the same signature | Yes, if not final / private |
| Use for | Factories, stateless functions, main | Behaviour that depends on object state |
class Counter {
int n;
static int total;
void inc() { n++; total++; } // OK
static int current() { return n; } // compile error: no this
static int grand() { return total; } // OK
}
Production pitfall: instance.staticMethod() compiles but lies to the reader. Always write ClassName.staticMethod(). Static state is shared process-wide — a common source of test pollution and hidden coupling.
Definition: multiple methods in the same class (or inherited, visible) share a name and have different signatures. This is compile-time (static) polymorphism: the compiler picks an overload using the compile-time types of the arguments.
throws alone cannot.void f(List<String>) and void f(List<Integer>) are the same signature — they do not compile together.void log(String s) { }
void log(int n) { }
void log(String s, int n) { }
Definition: every argument is a copy of the caller’s value. For primitives that value is the number/boolean. For references that value is the reference (a pointer), not a deep copy of the object.
School texts say “call by reference for objects.” That is misleading. Reassigning the parameter never rebinds the caller’s variable. Mutating the object the reference points at is visible to the caller because both variables point at the same object.
void rebind(String[] xs) {
xs = new String[] { "nope" }; // caller unchanged
}
void mutate(String[] xs) {
xs[0] = "yes"; // caller sees this
}
void rebindPrimitive(int n) {
n = 99; // caller unchanged
}
Professional language: Java is pass-by-value; object references are values. There is no C++-style reference parameter and no swap(a, b) that rebinds two caller variables.
The compiler chooses the most specific applicable method. If two are equally specific, the call is ambiguous and does not compile.
int → long) beats boxing (int → Integer).void f(long n) { }
void f(Integer n) { }
f(1); // long — widening beats boxing
void g(int... xs) { }
void g(Integer x) { }
g(1); // Integer — fixed arity beats varargs
void h(String s) { }
void h(Object o) { }
h(null); // String — more specific reference
void k(String s) { }
void k(Integer n) { }
k(null); // compile error: ambiguous
null is a valid value of every reference type, so it often makes overloads ambiguous. Autoboxing can also pick a surprising overload when you add a new method later — that is a source-incompatible API change.
void f(String... names) is compiled as String[]. Callers may pass an array or a sequence. A null array argument is a null array, not an array of one null.
void f(String... names) { }
f(); // length 0
f("a", "b"); // length 2
f(new String[]{"a"});
f(null); // names == null — NPE if you iterate blindly
f((String) null); // length 1, names[0] == null
f(String) and f(String...) is legal but hostile: f("a") binds the non-varargs method.T...) can warn; prefer overloads or List<T> in public APIs.this).Objects.requireNonNull, explicit range checks, or a small private guard. Do not fail halfway through a mutation.equals(Object) vs equals(MyType) is a famous trap — the latter does not override).this (instance) or the Class object (static). That publishes your lock. Prefer a private final lock object for non-trivial classes.// Incorrect — overloads Object.equals, does not override it
public boolean equals(Person other) { return other != null && id.equals(other.id); }
// Correct
@Override
public boolean equals(Object other) { /* ... */ }
equals with a tighter parameter type.List<T> over public generic varargs. Keep main thin: parse args, then call real methods you can test.~ Good luck with your revision! ~