• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best Way of Accessing Constants

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic