2.6.11 Debugging and Tracing Constructor Execution

Debugging and Tracing Constructor Execution

Use a breakpoint to observe when constructor parameters become stored Player state. The highlighted line is normally the next statement to execute, not evidence that it has already run.

Trace a complete known constructor

Use the complete Player definition from the parameterized-constructor lesson.

Statement to execute inside a caller method
Player jordan = new Player("Jordan", 7);
  1. Build the project. Put a breakpoint on this.name = name; in the Player constructor.
  2. Run the caller so it creates Jordan. When execution pauses, inspect name, jerseyNumber, and the fields under this in Locals.
  3. Use Step Over to execute the name assignment, then the jersey assignment.
  4. Continue back to the caller and inspect jordan after the creation statement has completed. Remove the breakpoint when finished.
Match values to execution moments
Expected debugger observations
Paused moment Parameter name Stored name Stored jerseyNumber
Before first assignment Jordan null 0
After name assignment Jordan Jordan 0
After jersey assignment Jordan Jordan 7

These are expected observations for the linked class, whose fields have no initializers. Locals formatting can differ by IDE version. Expand this to distinguish the object fields from same-named parameters.

Follow this across different creations

Create Casey next with jersey 12. The constructor runs again with different parameters and a different this reference. Jordan retains its own state.

Apply the idea
Check your understanding