aspose file tools
The moose likes Java in General and the fly likes which is the best way to declare constants? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "which is the best way to declare constants?" Watch "which is the best way to declare constants?" New topic
Author

which is the best way to declare constants?

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi,

There are many ways to declare constants in a java program.
1st way is writing a the constants in java program file.
2nd way is writing a configuaration file using XML document.
3rd ways is flat file using text file (properties).

But i am unable to decide which is the better among these 3



please any experienced programmers can give me some suggestions

thanks in advance


regards
saikrishna


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I would say it depends where exactly are you trying to use that constant and how often it's (if ever) value would change.

For example writing an email address in the web.xml would make more sense and not in a class file.

Then in case you define a constant like PI it would make more sense to define it at the class level. Like java.lang.Math.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If the "constants" are truly constant and will never vary, then coding them as "static final" Java fields is the way to go. It would be pointless overhead to put them in a file.

If the "constants" are in fact configuration parameters, which are constant during a particular run but might vary between runs, then a configuration file could be appropriate. Properties files are simple and cheap, but specific to Java. XML is a bigger overhead, but interoperates with all sorts of other tools; only you can decide whether that makes it worthwhile.

You could also consider the Java Configuration API, or a database, or a UI or...
[ February 28, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
But in java code if i keep constants i need to compile the java file.

i think using flat files is better than writing the constant in a java file?

and the flat files need not require additional stuff(jar files) to read the file. :roll:

am i right?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Again, if the "constants" are really constant, you won't be changing them. Therefore, you can compile the Java source file containing them as part of the ordinary build of your product. You'll never have to re-build just to change the constants, because the constants don't change.

If the "constants" are not really constant - e.g. they're user-adjustable parameters - then putting them in Java source may be inappropriate as, like you say, you'll need to rebuild to change them.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Peter Chase:
Again, if the "constants" are really constant, you won't be changing them. Therefore, you can compile the Java source file containing them as part of the ordinary build of your product. You'll never have to re-build just to change the constants, because the constants don't change.

If the "constants" are not really constant - e.g. they're user-adjustable parameters - then putting them in Java source may be inappropriate as, like you say, you'll need to rebuild to change them.


ok thanks!

i will use java file for static final variables

Good explanation

Richard Thatch
Greenhorn

Joined: Feb 16, 2007
Posts: 12
one other approach is to put it on the command line

either as a -D parameter or something that can be parsed in through args...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: which is the best way to declare constants?
 
Similar Threads
Need to read XMI file through java code
Java Constants equiv in XML Schema?
Variables in interfaces
Question about Tree Node --Word Tree
Constants