Properties, defaults not working

G

Guest

Hi!

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."

and:

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

But when I write the following:

import java.util.*;
import java.io.*;
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.load(propfile))
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
Martin

PS: Since I am not using my own email to avoid spam, you can write
directly to my email (remove spaces!) martin.wittrup @ jubiimail.dk
if you prefer to send direct response.
 
A

Andrey S. Vakhnin

Hello (e-mail address removed)

Hi!

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."

and:

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

But when I write the following:

import java.util.*;
import java.io.*;
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.load(propfile))
props.setProperty("Test2","New value for test2");

System.out.println("Test1 : "+props.get("Test1"));
System.out.println("Test2 : "+props.get("Test2"));

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

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

}
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
Martin

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

WBR,
Andrey Vakhnin
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top