Java Object Construction

Choosing how an object comes into existence in Java: static factory versus public constructor, the of/from/valueOf/getInstance naming conventions, instance control (caching, canonicalisation, value-based classes), enum and holder singletons, noninstantiable utility classes, and passing collaborators in rather than hardwiring them with new. Use when a class has several constructors distinguished only by parameter types, when a constructor does work beyond assigning fields, when a singleton or a static mutable field is proposed, when new appears inside domain logic for something the code can never substitute in a test, or when a factory hands back a type its callers should not be able to name. Does not cover builders and fluent chains (java-fluent-apis), which dependency edge should exist at all (java-dependency-inversion), defensive copying of components (java-immutability), or releasing what construction acquires (java-resource-management).

robsonkades 319b811 4 files · 29.1 KB Updated

File contents

robsonkades/agent-skills/tree/main/skills/java-object-construction commit 319b811703

Frequently asked questions

npx skillmds@latest add robsonkades/java-object-construction