0
0
Javaprogramming~10 mins

Object interaction in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object interaction
Create Object A
Create Object B
Object A calls method on Object B
Object B executes method
Return result to Object A
Object A uses result
This flow shows how one object creates another, calls its method, and uses the returned result.
Execution Sample
Java
class Printer {
  void printMessage(String msg) {
    System.out.println(msg);
  }
}

class User {
  void sendPrint(Printer p) {
    p.printMessage("Hello from User");
  }
}

public class Main {
  public static void main(String[] args) {
    Printer printer = new Printer();
    User user = new User();
    user.sendPrint(printer);
  }
}
User object calls a method on Printer object to print a message.
Execution Table
StepActionObjectMethod CalledParameterOutput
1Create Printer objectPrinterN/AN/APrinter instance created
2Create User objectUserN/AN/AUser instance created
3User calls sendPrintUsersendPrintPrinter objectNo output
4sendPrint calls printMessagePrinterprintMessage"Hello from User"Prints: Hello from User
5printMessage completesPrinterprintMessageN/AMethod returns void
6sendPrint completesUsersendPrintN/AMethod returns void
7main completesMainmainN/AProgram ends
💡 Program ends after main method completes
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
printernullPrinter instancePrinter instancePrinter instancePrinter instance
usernullnullUser instanceUser instanceUser instance
Key Moments - 3 Insights
Why does User need a Printer object to call printMessage?
Because printMessage belongs to Printer, User must have a reference to Printer to call its method, as shown in step 3 of the execution_table.
Does printMessage return any value to sendPrint?
No, printMessage returns void (no value), so sendPrint just calls it and continues, as seen in steps 4 and 5.
What happens if User tries to call printMessage without a Printer object?
It causes an error because printMessage is not in User; User must have a Printer reference to call it, shown by the need for parameter in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when printMessage is called?
ANo output
BPrints: Hello from User
CReturns a string
DThrows an error
💡 Hint
Check step 4 in the execution_table where printMessage prints the message.
At which step does the User object call a method on the Printer object?
AStep 2
BStep 5
CStep 3
DStep 7
💡 Hint
Look at step 3 in the execution_table where sendPrint is called with Printer as parameter.
If the Printer object was not created, what would happen at step 3?
ANullPointerException error occurs
BsendPrint calls printMessage anyway
CUser calls sendPrint successfully
DProgram prints a default message
💡 Hint
Refer to variable_tracker and step 3 in execution_table; without Printer instance, calling method causes error.
Concept Snapshot
Object interaction in Java:
- One object holds reference to another
- Calls methods on that object using dot notation
- Methods can return values or be void
- Objects must be created before interaction
- Example: user.sendPrint(printer) calls printer.printMessage()
Full Transcript
This example shows how two objects interact in Java. First, a Printer object is created, then a User object. The User object calls its sendPrint method, passing the Printer object. Inside sendPrint, the Printer's printMessage method is called with a message. The message is printed to the console. The methods return void, so no values are passed back. The program ends after main completes. This demonstrates how objects communicate by calling each other's methods using references.