Bird
Raised Fist0
Javaprogramming~10 mins

Method calling in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Method calling
Start main method
↓
Call method with arguments
↓
Enter method
↓
Execute method body
↓
Return result (if any)
↓
Resume main method
↓
End program
This flow shows how the main method calls another method, executes it, and then returns to continue.
Execution Sample
Java
public class Test {
  public static void main(String[] args) {
    int result = add(3, 4);
    System.out.println(result);
  }
  public static int add(int a, int b) {
    return a + b;
  }
}
This code calls the add method with 3 and 4, then prints the sum.
Execution Table
StepActionMethodParametersReturn ValueOutput
1Start main methodmainargsN/AN/A
2Call add methodadda=3, b=4N/AN/A
3Execute add method bodyadda=3, b=47N/A
4Return to main methodmainN/A7N/A
5Print resultmainN/AN/A7
6End programmainN/AN/AN/A
💡 Program ends after printing the result.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
resultundefinedundefined777
aN/A33N/AN/A
bN/A44N/AN/A
Key Moments - 3 Insights
Why does the program jump to the add method when it is called?
When main calls add (see Step 2 in execution_table), the program temporarily pauses main and runs add with given parameters.
What happens to variables a and b after add finishes?
Variables a and b exist only inside add method (Step 3). After returning, they are gone and main continues.
Why does main store the return value from add in result?
Because add returns a value (7), main saves it in result to use later (Step 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after Step 3?
Aundefined
B3
C7
D4
💡 Hint
Check the variable_tracker row for 'result' after Step 3.
At which step does the program print the output?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Output' column in execution_table.
If add method returned a + b + 1 instead, what would 'result' be after Step 3?
A8
B6
C7
D4
💡 Hint
Add 3 + 4 + 1 equals 8, check variable_tracker logic.
Concept Snapshot
Method calling in Java:
- Use methodName(arguments) to call a method.
- Program jumps to method, runs code, then returns.
- Returned value can be stored in a variable.
- Parameters are local to the method.
- After return, program continues where it left off.
Full Transcript
This example shows how Java calls a method named add from main. The main method starts and calls add with 3 and 4. The program jumps to add, runs its code to add the two numbers, and returns 7. Main receives 7 and stores it in result. Then main prints 7 and ends. Variables a and b exist only inside add. This flow helps understand how methods work step-by-step.

Practice

(1/5)
1. What does calling a method in Java do?
myMethod();
easy
A. Runs the instructions inside the method named myMethod.
B. Creates a new method called myMethod.
C. Deletes the method myMethod from the program.
D. Prints the name myMethod on the screen.

Solution

  1. Step 1: Understand what a method call does

    Calling a method means running the code inside that method.
  2. Step 2: Analyze the example myMethod();

    This runs the instructions saved inside the method named myMethod.
  3. Final Answer:

    Runs the instructions inside the method named myMethod. -> Option A
  4. Quick Check:

    Method call = run method code [OK]
Hint: Method call runs code inside method name() [OK]
Common Mistakes:
  • Thinking method call creates or deletes methods
  • Confusing method call with printing text
  • Assuming method call changes method definition
2. Which of the following is the correct way to call a method named displayMessage in Java?
easy
A. run displayMessage();
B. call displayMessage();
C. displayMessage;
D. displayMessage();

Solution

  1. Step 1: Recall Java method call syntax

    In Java, you call a method by writing its name followed by parentheses.
  2. Step 2: Check each option

    Only displayMessage(); matches the correct syntax for calling a method.
  3. Final Answer:

    displayMessage(); -> Option D
  4. Quick Check:

    Method call syntax = name() [OK]
Hint: Method call needs parentheses after name [OK]
Common Mistakes:
  • Omitting parentheses when calling methods
  • Adding extra words like 'call' or 'run'
  • Using semicolon without parentheses
3. What is the output of this Java code?
public class Test {
  public static void sayHi() {
    System.out.println("Hi there!");
  }
  public static void main(String[] args) {
    sayHi();
  }
}
medium
A. sayHi()
B. No output
C. Hi there!
D. Compilation error

Solution

  1. Step 1: Identify the method call in main

    The main method calls sayHi(), which prints "Hi there!".
  2. Step 2: Understand what sayHi() does

    It prints the text "Hi there!" to the screen.
  3. Final Answer:

    Hi there! -> Option C
  4. Quick Check:

    Method prints text = output text [OK]
Hint: Method prints text, so output is printed string [OK]
Common Mistakes:
  • Confusing method name with output
  • Expecting no output without print statement
  • Thinking method call causes error
4. Find the error in this Java code snippet:
public class Demo {
  public static void greet() {
    System.out.println("Hello!");
  }
  public static void main(String[] args) {
    greet;
  }
}
medium
A. Incorrect method name.
B. Missing parentheses when calling method greet.
C. Missing semicolon after greet.
D. Method greet is not defined.

Solution

  1. Step 1: Check method call syntax in main

    The code uses greet; which is missing parentheses.
  2. Step 2: Understand correct method call

    Method calls require parentheses even if no arguments: greet();.
  3. Final Answer:

    Missing parentheses when calling method greet. -> Option B
  4. Quick Check:

    Method call needs () [OK]
Hint: Always use () to call methods, even if empty [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Thinking semicolon alone calls method
  • Assuming method call without () is valid
5. Given this Java code:
public class Calculator {
  public static int add(int a, int b) {
    return a + b;
  }
  public static void main(String[] args) {
    int result = add(5, 3);
    System.out.println(result);
  }
}

What will be printed when the program runs?
hard
A. 8
B. 53
C. add(5, 3)
D. Compilation error

Solution

  1. Step 1: Understand method add parameters and return

    The method add takes two integers and returns their sum.
  2. Step 2: Analyze the call add(5, 3)

    It returns 5 + 3 = 8, which is stored in result.
  3. Step 3: Check the output statement

    System.out.println(result); prints the value 8.
  4. Final Answer:

    8 -> Option A
  5. Quick Check:

    Sum of 5 and 3 = 8 [OK]
Hint: Method returns sum; print shows result [OK]
Common Mistakes:
  • Concatenating numbers as strings instead of adding
  • Expecting method name printed instead of result
  • Confusing return value with method call syntax
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