views:

61

answers:

4

I develop a webservice application in a tomcat container, I have a lot of properties for the webapp like constants, error messages and so on.

What is the better and faster way to?

+1  A: 

public static final String SOME_CONSTANT = "contstantValue";

This makes the compiler inline the value, so you have nothing happening at runtime.

You can also store the values anywhere (.properties file) and load them in a static initializer block.

Bozho
+4  A: 

The answer, as always, is profile it.

In general, this is the sort of micro-optimization that almost certainly won't matter. Feel free to profile it and see, though.

Hank Gay
Agree. Don't focus on optimizing things of which you don't know that they actually cause a performance problem. The only good way to optimize is to measure where the bottlenecks are, and concentrate on improving the code that is the bottleneck.
Jesper
+1, profiling is the best way to answer the "faster" part of the question. The "better" part needs analysis/insight, though. :-)
T.J. Crowder
Profiling is irrelevant -- doing I/O when you would have been fine with plain old constants is never going to be better.
Kevin Bourrillion
@Kevin Bourrillion - that's true, but do you honestly believe for a single instant that *this* is where a performance bottleneck is at? The time spent asking (and answering) the question would have been better spent finding what is *actually* making the system run slower and working on that.
Hank Gay
Oh, I couldn't agree with that more. I just get tired of saying it over and over and over.
Kevin Bourrillion
+1  A: 

Generally static constants; IO activity will always be slower.

However, hard-coding values means that the app will need to be recompiled if the value has to change. It's your call as a developer as to whether the values will ever need to be changed outside of a software release.

David Neale
+1  A: 

As Bozho says, in terms of raw speed, you're going to have trouble beating a public static final. But speed isn't everything. If you need to worry about localization at all, for instance, a properties file would probably be better, although you might want to look at a ResourceBundle instead.

T.J. Crowder