7 Replies Latest reply on Mar 5, 2010 8:59 AM by darrylburke

    java.util.Properties: why are defaults stored in a separate field?

      I am writing a replacement class for the hoary java.util.Properties, which will hopefully address its shortcomings.

      One design decision in Properties has me puzzled, and I would love your insight. Properties has a protected field called defaults that is another Properties instance. It can only be set in one of the constructors. It serves as a source of default properties. For example, if a call to getProperty does not immediately find the key in this Properties instance, then it will recursively go up the chain of defaults fields searching for key.

      I do not see why you would want to store the original source of default name/value pairs in that field. It has the following issues:

      1) you need to write recursive algorithms to access all of the data; this is a bit tricky to code (and to javadoc: the javadocs of Properties are badly written, and do not always clearly indicate if the complete defaults chain is recursed, or just the immediate defaults field; I had to check the source code to determine...)

      2) retaining a reference to the default Properties instance keeps it from being garbage collected

      What my class does is have a constructor which takes a generic Map<String,String> param and simply copies all of the key/value pairs into itself. But the Map param is otherwise unused. If some user subsequently calls put(key, value) and it overwrites some initialized value, well so be it.

      Am I overlooking something here? Is there, in fact, a good reason why Properties was written the way it was?