Skip to the solution
EasyFrontend6 min read

What are static methods and properties?

927 views

The problem

Question presented to candidate: "If you have a class with a static counter property that increments in the constructor every time a new instance is created, and you create 3 instances, what does the counter equal β€” and can you access that counter from one of the instances directly?"

What a strong answer should cover:

  • πŸ“Œ Interview term: static β€” a class member (method or property) prefixed with static belongs to the class itself, not to any individual instance β€” there is genuinely only ONE copy, shared across every instance, not a separate copy per instance.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a static count property genuinely incremented to 3 after 3 real instantiations, since every instance's constructor updated the SAME single static value β€” and that static property is genuinely not accessible directly on an instance (instance.increment is undefined), only on the class itself (Counter.increment).
  • πŸ“Œ Interview term: static initialization blocks (ES2022) β€” verified directly: a static { ... } block genuinely runs once, at class-definition time, useful for static properties needing more complex setup logic than a single expression can provide.
  • πŸ“Œ Interview term: static inheritance β€” verified directly: a subclass genuinely inherits its parent's static methods β€” calling a static method on the SUBCLASS that was only defined on the parent genuinely works.
  • A precise answer names the real, common use cases for static members: factory methods (ClassName.create(...)), utility/helper methods that don't need any specific instance's state, and shared counters/registries tracking something across every instance β€” exactly the pattern verified in this answer's own counter example.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering the prompt's exact scenario (the shared count, and instance-inaccessibility) is the strong signal.

Code / implementation expected: Yes β€” reproducing the prompt's exact counter scenario, plus verifying instance-inaccessibility, is the clearest, most convincing demonstration.

  • classes
  • static

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a static counter is genuinely shared across all instances, and genuinely inaccessible directly on any one instance

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 11 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What are optional chaining and nullish coalescing?

Continue