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.

The lesson behind it →
More on Static members