Methods
A method is a named block of code with parameters going in and a result coming out. Methods remove repetition and give a piece of logic a name by which it can be called and tested on its own. This topic is about static methods, which are called without an object, like main; object methods, overloading and overriding are covered in OOP.
The main idea of the topic is that every call works with its own copies. Parameters receive copies of the arguments, and local variables live in the call's own stack frame and disappear once the method returns. All the traps follow from this model: a method cannot reassign the caller's variable, recursion without a base case overflows the stack, and a local variable with the same name in a nested block does not compile.
Topic map
- Declaring and Calling — the signature, parameters,
returnandvoid, calling frommainand themissing return statementerror. - Pass by Value — a method gets a copy of the argument; for an object it is a copy of the reference.
- Recursion — the base case, the call stack and
StackOverflowError. - Scope — where a local variable is visible, blocks, and the ban on declaring a name again.
Common traps
| Mistake | Consequence |
|---|---|
| Returning a value on some paths of a method only | It does not compile: missing return statement |
Calling a non-static method from main without an object | It does not compile: non-static method cannot be referenced from a static context |
| Changing a primitive parameter to change the caller's variable | The copy changes; the variable outside stays the same |
| Believing Java passes objects by reference | A copy of the reference is passed: changes to the object are visible, reassigning the parameter is not |
| Writing recursion without a base case | Calls pile up on the stack until StackOverflowError |
| Declaring a variable in a nested block with an outer variable's name | It does not compile: variable is already defined |
Reading a variable declared inside an if after the block | It does not compile: cannot find symbol |
Interview relevance
Interviews check methods through parameter passing: "what does this code print if the method changes its parameter", "why does swap not work", "does Java pass objects by reference". The right answer is always the same — Java passes everything by value — but the point is to explain the mechanism: the value of a variable of an object type is a reference, and the method gets a copy of it.
Typical checks:
- What a method signature consists of and why the return type is not part of it.
- Why the
staticmethodmaincannot call an instance method without an object. - Pass-by-value for primitives, arrays and objects, and the
swapthat does not work. - Recursion: the base case, stack depth,
StackOverflowErroras anError, not anException. - The scope of local variables and the ban on declaring them again in nested blocks.
Common wrong answer: "Primitives are passed by value, and objects by reference." That opens the discussion that with pass-by-reference a method could replace the caller's object, while in Java assigning to the parameter, p = new Point(), is not visible outside — only the effects of calling methods on the same object are.