Hi I got the code from a book:
public class Container {
Map<String, Object> components;
public Container() {
components = new HashMap<String, Object>();
Properties properties = new Properties();
try {
properties.load(new FileInputStream("components.properties"));
for (Map.Entry entry : properties.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
processEntry(key, value);
}
} catch (Exception ex) {
throw new RuntimeException();
}
}
private void processEntry(String key, String value) throws Exception {
String parts[] = key.split("\\.");
if (parts.length == 1) {
Object component = Class.forName(value).newInstance();
components.put(parts[0], component);
} else {
Object component = components.get(parts[0]);
Object reference = components.get(value);
PropertyUtils.setProperty(component, parts[1], reference);
}
}
public Object getComponent(String id) {
return components.get(id);
}
}
My question is, on the line
PropertyUtils.setProperty(component, parts[1], reference);
The object's property in the Map is changed. Even though after the property is updated there's no component.put() to update the object inside the map, the object is updated. Why is that?