• Post Reply Bookmark Topic Watch Topic
  • New Topic

in the my book appears above, well Properties(); key, value , is it REQUIRED to be <String,String>?  RSS feed

 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
table = new Properties();
// set properties
table.setProperty( "color", "blue" );
table.setProperty( "width", "200" );

in the my book appears above, well Properties(); key, value , is it REQUIRED to be <String,String>? or only key be String? or ...
 
Michael Angstadt
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseAMeaningfulSubjectLine.

The Properties class only accepts Strings as keys and values.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. Due to some unexpected (at least to me) decision, when generics were introduced Sun decided to let Properties extend Hashtable<Object,Object>, not Hashtable<String,String>. This means that although setProperty only lets you use Strings, the put method still is available and lets you use any object for both key and value except null (since Hashtable does not allow null keys or values).

This class is just an example of bad OO in Sun's early classes. Properties should never have extended Hashtable but use one internally. Likewise, java.util.Stack never should have extended Vector but again use one internally. We'll have to live with that, at least in the case of Properties. Which, I repeat, should have extended Hashtable<String,String>, not Hashtable<Object,Object>. Anyone using a Properties object to store objects other than Strings is abusing the bad OO and should be punished by having to rework his code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!