Properties, defaults not working

Discussion in 'Java' started by Guest, Sep 30, 2003.

  1. Guest

    Guest Guest


    Can anyone explain why the JavaDoc on Properties says:

    "A property list can contain another property list as its "defaults";
    this second property list is searched if the property key is not found
    in the original property list."


    public Properties(Properties defaults)
    Creates an empty property list with the specified defaults.

    But when I write the following:

    import java.util.*;
    public class PropTest{

    private Properties props = null;

    public PropTest(){

    // Set defaults
    Properties defaults = new Properties();
    defaults.setProperty("Test1", "Default value for Test1");

    // Create global properties with defaults
    props = new Properties(defaults);

    // Set property (same result as with
    props.setProperty("Test2","New value for test2");

    System.out.println("Test1 : "+props.get("Test1"));
    System.out.println("Test2 : "+props.get("Test2"));
    public static void main(String[] args){
    PropTest p = new PropTest();

    I get the output:

    Test1 : null
    Test2 : New value for test2

    where I would expect Test1 to yield: "Default value for Test1"

    I am using java version 1.4.1_01_b01

    Thanks in advance for any comments!

    Best regards

    PS: Since I am not using my own email to avoid spam, you can write
    directly to my email (remove spaces!) martin.wittrup @
    if you prefer to send direct response.
    Guest, Sep 30, 2003
    1. Advertisements

  2. Hello

    Try to use:
    System.out.println("Test1 : "+props.getProperty("Test1"));

    Method get is inherited from HashTable, which doesn't know anything about
    default properties :)

    Andrey Vakhnin
    Andrey S. Vakhnin, Sep 30, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.