Skip to main content

Practice ยท 3 of 3

The container core

Inside Solution, write public static class Container: 1. public <T> T resolve(Class<T> type) โ€” recursive constructor injection: pick the first declared constructor, resolve EVERY parameter type the same way, instantiate with ctor.newInstance(...), cast back via type.cast(...). 2. Singleton scope: resolving the same type twice returns the SAME instance (cache by Class token โ€” Module 6's heterogeneous map). 3. Cycle detection: if resolution of A reaches A again, throw IllegalStateException with a message containing "circular". Supports: nested classes you define are resolved through their constructors.

Difficulty: advanced

Back to lesson: Practice: Reflection & DI drills