Skip to solution
easyFrontend

What are static methods and properties?

927 views
01

Understand 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.

classesstatic
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals/OOP interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every shared-state and accessibility claim below was actually run in Node.

1. Why T

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 11 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track