Trace Java Static and Instance State Across Constructors

Read the full interview experience this question came from →

Quick Overview

Explain Java static and instance field state across constructor calls, including initialization, shared values, copied values, and object references.

Trace Java Static and Instance State Across Constructors

Company: Pinduoduo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

A Java class contains both static fields and instance fields. Its constructor changes values in both kinds of fields. A static `main` method constructs multiple objects and later reads fields through the class and through those objects. Explain how to reason about the values that will be observed. Distinguish shared class state from per-object state, describe initialization relative to constructor execution, and explain why constructing a later object can change some values observed through an earlier object while leaving others unchanged. ### Constraints and Clarifying Questions - Assume ordinary single-threaded execution of one class whose direct superclass is `Object`, with no custom class loading. - Each constructor either relies on implicit `super()` or places explicit `super()` first. There is no constructor prologue before that invocation and no delegation through `this(...)`. - The constructor may assign to or update fields; do not assume every update is an increment. - Distinguish reading a static field from copying its current value into an instance field. - To compute a numerical printed output, the actual initializers, assignments, construction order, and reads must be supplied. This question asks for the state model and tracing method, not unspecified numerical output. ```hint Draw separate state locations Give the class one shared field area and each constructed object its own field area. For each constructor statement, decide which location is read and which location is changed. ``` ### What a Strong Answer Covers - Storage and ownership of static fields versus instance fields. - Default values, field initializers, and constructor assignments in the stated single-class setting. - The state changes produced by successive constructions and later field reads. - Why an instance field holding an earlier copied value does not automatically track later changes to a static field. ### Follow-up Questions - If a constructor assigns a fixed value to a static field, does constructing another object necessarily increase that value? - If two reference variables point to the same object, do they have separate instance-field state? - Why can reading a static field through an object reference be misleading even when Java permits that syntax?

Overview: Explain Java static and instance field state across constructor calls, including initialization, shared values, copied values, and object references.

Read the full Pinduoduo Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/Pinduoduo
Pinduoduo logo
Pinduoduo
Aug 22, 2026
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

A Java class contains both static fields and instance fields. Its constructor changes values in both kinds of fields. A static main method constructs multiple objects and later reads fields through the class and through those objects.

Explain how to reason about the values that will be observed. Distinguish shared class state from per-object state, describe initialization relative to constructor execution, and explain why constructing a later object can change some values observed through an earlier object while leaving others unchanged.

Constraints and Clarifying Questions

  • Assume ordinary single-threaded execution of one class whose direct superclass is Object , with no custom class loading.
  • Each constructor either relies on implicit super() or places explicit super() first. There is no constructor prologue before that invocation and no delegation through this(...) .
  • The constructor may assign to or update fields; do not assume every update is an increment.
  • Distinguish reading a static field from copying its current value into an instance field.
  • To compute a numerical printed output, the actual initializers, assignments, construction order, and reads must be supplied. This question asks for the state model and tracing method, not unspecified numerical output.

What a Strong Answer Covers Guidance

  • Storage and ownership of static fields versus instance fields.
  • Default values, field initializers, and constructor assignments in the stated single-class setting.
  • The state changes produced by successive constructions and later field reads.
  • Why an instance field holding an earlier copied value does not automatically track later changes to a static field.

Follow-up Questions Guidance

  • If a constructor assigns a fixed value to a static field, does constructing another object necessarily increase that value?
  • If two reference variables point to the same object, do they have separate instance-field state?
  • Why can reading a static field through an object reference be misleading even when Java permits that syntax?
Loading comments...