Hi,
I'd like to get people's take on the best way of accessing constant variables such as the driver name, urls, and the like that need to be accessed by many classes.
A couple of ways are:
1) storing them in an interface as static and final then get the class to implement that interface
2) reading properties from a configuration file
I think the interface case is faster. I did a
test case by running Comparer (see below) and the Interface was heads above the Properties example.
---------------------------------------
public class Comparer {
static public void main(
String [] args) {
long pre = 0;
pre = System.currentTimeMillis();
for(int i = 0; i < 3000; i++){
inter ii = new inter();
ii.m();
}
System.out.println("inter=" + (System.currentTimeMillis() - pre));
pre = System.currentTimeMillis();
for(int i = 0; i < 3000; i++){
prop d = new prop();
d.m();
}
System.out.println("properties=" +(System.currentTimeMillis() - pre));
}
}
---------------------------------
public interface STANDARDS {
public static final String URL = "www.hotmail.com";
}
public class InterfaceExample implements STANDARDS {
public void m() {
String T = URL;
}
}
---------------------------------
import java.io.*;
import java.util.*;
public class PropertiesExample {
public void m() {
String url = null;
Properties prop = new Properties();
File config = new File("config.txt");
try {
prop.load(new FileInputStream(config));
} catch (IOException e) {}
url = prop.getProperty("url");
}
}
Looking forward to hearing what people think.
Yoo-Jin.