• 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

which is the best way to declare constants?

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic