Bird
Raised Fist0
Javaprogramming~10 mins

Method declaration in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Method declaration
Start
↓
Write method signature
↓
Add method body with statements
↓
Method ready to be called
↓
End
This flow shows how a method is declared: first write the signature, then add the body, and finally the method is ready to use.
Execution Sample
Java
public int add(int a, int b) {
    return a + b;
}
This code declares a method named add that takes two numbers and returns their sum.
Execution Table
StepActionEvaluationResult
1Declare method 'add' with parameters a=3, b=5Method signature acceptedMethod ready
2Execute method body: return a + bCalculate 3 + 58 returned
3Method call completesReturn value 8Output = 8
💡 Method execution ends after returning the sum of a and b
Variable Tracker
VariableStartDuring ExecutionFinal
aundefined33
bundefined55
return valueundefined88
Key Moments - 3 Insights
Why do we need to specify the return type before the method name?
The return type tells Java what kind of value the method will give back. In the execution_table step 2, it shows the method returns an int, so Java knows to expect a number.
What happens if we forget to write 'return' inside the method?
Without 'return', the method does not send back a value. The execution_table step 2 shows the return statement is needed to produce the result.
Can the method parameters change during execution?
No, parameters like 'a' and 'b' keep their values during the method call, as shown in variable_tracker where 'a' and 'b' stay 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value at step 2?
A3
B8
C5
Dundefined
💡 Hint
Check the 'Result' column in execution_table row for step 2
At which step does the method become ready to be called?
AStep 1
BStep 2
CStep 3
DAfter Step 3
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for step 1
If we change the return type to void, what changes in the execution?
AMethod returns a number
BMethod parameters change
CMethod does not return any value
DMethod signature is invalid
💡 Hint
Recall that 'void' means no return value, so check variable_tracker for 'return value'
Concept Snapshot
Method declaration syntax:
[access_modifier] [return_type] methodName([parameters]) {
    // method body
}
- Return type tells what value method sends back
- Parameters are inputs to method
- Method body contains code to run
- Use 'return' to send back a value
Full Transcript
This visual trace shows how a method is declared in Java. First, the method signature is written with access modifier, return type, name, and parameters. Then the method body contains statements, here a return statement adding two numbers. The execution table traces declaring the method, running the body, and returning the result. Variables a and b hold input values, and the return value is the sum. Key points include the need for return type and return statement. The quizzes check understanding of return values, method readiness, and effects of changing return type.

Practice

(1/5)
1. What is the correct way to declare a method in Java that returns an integer and takes no parameters?
easy
A. int getNumber { return 5; }
B. public int getNumber() { return 5; }
C. public getNumber() int { return 5; }
D. public int getNumber[] { return 5; }

Solution

  1. Step 1: Check method signature format

    The correct method declaration includes access modifier, return type, method name, and parentheses for parameters.
  2. Step 2: Validate syntax correctness

    public int getNumber() { return 5; } correctly uses 'public int getNumber()' with parentheses and a return statement.
  3. Final Answer:

    public int getNumber() { return 5; } -> Option B
  4. Quick Check:

    Method declaration = public int getNumber() { ... } [OK]
Hint: Method declaration needs return type, name, and parentheses [OK]
Common Mistakes:
  • Omitting parentheses after method name
  • Missing return type before method name
  • Using brackets [] instead of parentheses for parameters
2. Which of the following is a correct method declaration syntax in Java?
easy
A. void printMessage(String message) { System.out.println(message); }
B. void printMessage(String message); { System.out.println(message); }
C. void printMessage String message { System.out.println(message); }
D. void printMessage(String message) System.out.println(message);

Solution

  1. Step 1: Identify correct method header and body syntax

    Method header must have parentheses around parameters and body enclosed in braces.
  2. Step 2: Check for syntax errors

    void printMessage(String message) { System.out.println(message); } correctly declares method with parameters in parentheses and body in braces.
  3. Final Answer:

    void printMessage(String message) { System.out.println(message); } -> Option A
  4. Quick Check:

    Method syntax = returnType name(params) { body } [OK]
Hint: Method parameters must be inside parentheses and body inside braces [OK]
Common Mistakes:
  • Adding semicolon after method header
  • Missing parentheses around parameters
  • Omitting braces for method body
3. What will be the output of the following code?
public class Test {
  public static int add(int a, int b) {
    return a + b;
  }
  public static void main(String[] args) {
    System.out.println(add(3, 4));
  }
}
medium
A. 0
B. 34
C. 7
D. Error

Solution

  1. Step 1: Understand method call and parameters

    The method add takes two integers and returns their sum.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Method add returns sum; 3 + 4 = 7 [OK]
Common Mistakes:
  • Confusing concatenation with addition
  • Expecting compile error due to static method
  • Ignoring return value and expecting no output
4. Identify the error in the following method declaration:
public int multiply(int x, int y)
  return x * y;
}
medium
A. Extra semicolon after return statement
B. Missing return type
C. Incorrect parameter types
D. Missing opening brace '{' after method header

Solution

  1. Step 1: Check method header and body braces

    The method header must be followed by an opening brace '{' to start the method body.
  2. Step 2: Identify missing brace

    The code lacks '{' after the method header, causing a syntax error.
  3. Final Answer:

    Missing opening brace '{' after method header -> Option D
  4. Quick Check:

    Method body must start with '{' [OK]
Hint: Method body always starts with '{' after header [OK]
Common Mistakes:
  • Forgetting opening brace '{'
  • Placing return statement outside method body
  • Confusing semicolon placement
5. You want to declare a method named filterPositive that takes an array of integers and returns a new array containing only the positive numbers. Which method declaration is correct?
hard
A. public int[] filterPositive(int[] numbers) { /* implementation */ }
B. public int filterPositive(int numbers[]) { /* implementation */ }
C. public void filterPositive(int[] numbers) { /* implementation */ }
D. public int filterPositive(int[] numbers[]) { /* implementation */ }

Solution

  1. Step 1: Determine return type and parameter type

    The method returns an array of integers, so return type must be int[]. The parameter is an int array.
  2. Step 2: Check syntax correctness

    public int[] filterPositive(int[] numbers) { /* implementation */ } correctly declares return type as int[] and parameter as int[]. Other options have wrong return types or parameter syntax.
  3. Final Answer:

    public int[] filterPositive(int[] numbers) { /* implementation */ } -> Option A
  4. Quick Check:

    Return type matches output array type [OK]
Hint: Return type must match method output type (int[] for array) [OK]
Common Mistakes:
  • Using int instead of int[] as return type
  • Incorrect parameter array syntax
  • Using void when method returns data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard