Method
Signatures
Learn how to read, write, and interpret the blueprint of every Java method: its access modifier, return type, name, and parameter list.
What Is a Method?
A method is a named block of code that performs a specific task. Think of it like a recipe: it has a name, a list of ingredients (parameters), and produces a result (return value).
What Is a Method Signature?
A method signature is the unique identity of a method. In Java, the signature consists of the method name and the ordered list of parameter types. The compiler uses signatures to distinguish one method from another.
Anatomy of a Method Header
The Five Parts of a Method Header
Return Types Explained
Every method must declare what type of value it returns. If the method performs an action without returning data, use void.
Parameters vs Arguments
Signature vs Header: Key Distinction
| Method Signature | Method Header | |
|---|---|---|
| Includes Name? | ✓ Yes | ✓ Yes |
| Includes Param Types? | ✓ Yes | ✓ Yes |
| Includes Param Names? | ✕ No | ✓ Yes |
| Includes Return Type? | ✕ No | ✓ Yes |
| Includes Access Modifier? | ✕ No | ✓ Yes |
| Used for Overloading? | ✓ Yes | ✕ No |
How to Read a Method Signature
Example 1: void Method
Example 2: Method with Return Value
Example 3: Method Overloading
static vs Non-static Methods
Access Modifiers in Detail
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
| public | ✓ | ✓ | ✓ | ✓ |
| protected | ✓ | ✓ | ✓ | ✕ |
| (default) | ✓ | ✓ | ✕ | ✕ |
| private | ✓ | ✕ | ✕ | ✕ |
Common Mistakes to Avoid
Summary: Method Signature Cheat Sheet
| Concept | What It Means | Example |
|---|---|---|
| Method Name | The identifier for the method | calculateArea |
| Parameter List | Types and names of inputs | (double l, double w) |
| Signature | Name + parameter types only | calculateArea(double, double) |
| Return Type | Type of value returned | double |
| Header | Full declaration line | public double calculateArea(...) |
| Overloading | Same name, different signatures | print(String) vs print(String, int) |
Question 1 of 5
Which of the following is the method signature for this method header?public static double average(int x, int y)
Question 2 of 5
Which of the following pairs of methods are correctly overloaded?
Question 3 of 5
In the call myObj.process("hello", 42), what are "hello" and 42?
Question 4 of 5
A method is declared as private void reset(). Which statement is true?
Question 5 of 5
Consider these two method headers:
public int compute(double x, int y)
public int compute(int y, double x)
Do these methods have different signatures?