Skip to content

08. Retrieving objects

Jim Riordan edited this page Aug 10, 2016 · 2 revisions

###Retrieving objects from a container

We've already seen that to retrieve objects from a container we use a get method. It's important to note that no dependency resolution will happen unless you ask for an object. The order of injection doesn't matter. So in the example from the First program section we could have declared our dependencies this way:

Container container = new SimpleContainer();
container.add(Car.class); // Car added before its collaborators 
container.add(Engine.class);
container.add(Wheels.class);
Car car = container.get(Car.class); // works fine 

You can call a get method many times and it will always return the same object.

Clone this wiki locally