Static membersmedium3-5 years
A library author insists `Registry.getInstance()` is a singleton — "one per JVM, that's what static means." Two web applications deployed in the same Tomcat each see a different set of registrations from the same library jar. Who's right?
Both, about different things, and the disagreement is really about what static actually scopes to. static does mean exactly one copy — but one copy per class, and a class is identified not just by its fully-qualified name but by which class loader loaded it. Tomcat gives each deployed web application its own class loader, and if the library jar sits inside each application's own WEB-INF/lib, each application's loader loads its own, separate Registry.class object, with its own, separate static fields — two distinct classes that happen to share a name and a bytecode file, not one shared class. "One per JVM" is the intuition people carry from single-class-loader programs; it was never actually what the language guaranteed.
PreviousA teammate asks why the codebase prefers `Money.of(500, EUR)` over `new Money(500, EUR)` everywhere, calling it "just a style preference." Is it?Next A `ReportView` holding an 8 MB rendered buffer registers `bus.subscribe(new Listener() { public void onEvent(Event e) { refresh(); } })` and is never explicitly unsubscribed. After a few hundred report screens, the heap is gone, and a heap dump's histogram shows `byte[]` as the biggest consumer — which is true and doesn't explain anything. What's actually holding the memory, and what's the one-word category of fix?
A teammate asks why the codebase prefers `Money.of(500, EUR)` over `new Money(500, EUR)` everywhere, calling it "just a style preference." Is it?A reporting service declares `private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyy-MM-dd HH:mm")` and reuses it across every request. It passes every test. Under 200 concurrent requests in production, about one row in a thousand carries a date from a *different* request, and occasionally a `NumberFormatException` comes out of `parse`. What's actually happening, and why is `static` — not `SimpleDateFormat` alone — the other half of the bug?