• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Way of Accessing Constants  RSS feed

 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was just a discussion on this at in the Advanced Java forum http://www.javaranch.com/ubb/Forum34/HTML/000338.html
Read the comments there. Those are more design issues.
Bottom line: don't use interfaces to hold constants.

In your code, the field beat out the property file, because each time you re-read the entire property file! The right way to use property files for properties which you use over and over, is to read the property into memory once (either on startup, or the first time it is accessed), and there after not have to re-load the file. Which method is right, or even doing what you did below depends on your particular usage model, when and how often will you need this information. (The same decision needs to be made for saving changes to properties for non-constants.)
Try the following (note I haven't actually compiled this)


Note also that timing tests like the one you used can vary based on the JVM/JIT and processor. See Java Platform Performance (Wilson, Kesselman), chapter 3 for more information about this.

--Mark
hershey@vaultus.com

[This message has been edited by Mark Herschberg (edited December 13, 2000).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!