Methods in Java — Java Technical Interview Questions & Answers
Methods in Java
Methods in Java
- A method is a reusable block that does one job. Parts: return type, name, parameters, body. void means do the work and return nothing. int square(int n) returns a value.
- Call it on an object: s.study(); or on the class if it is static: Math.max(2, 5). Keep methods small — one clear responsibility. Forgetting return in a non-void method is a compile error.
- In an interview, write square(int n) live and explain each word of the signature.
- Let's take this on the board with one tiny Main class — no extra files. Methods in Java — output: 25. square(5) is 5*5. Change to square(6) in your head — 36. Start from main, go line by line, and stop at each print. That output is the proof for Methods.
Technical Interview Tip: Explain signature: return type + name + params. Write square(int n).
Code Examples for Methods in Java
Example
public class Main {
static int square(int n) {
return n * n;
}
public static void main(String[] args) {
System.out.println(square(5));
}
}Methods in Java — output: 25. square(5) is 5*5. Change to square(6) in your head — 36.
How to Explain Methods in Java to an Interviewer
What is a method?
Reusable block One job
What does void mean?
Returns nothing
What is a method signature?
return type + name + parameters