• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Where to store constants?  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really doubt where to store the constants according to a good programming design.

Situation
----------
Here is my situation. i have an email address and my application has an email functionality to send out the email using this email address.

Right now, this email address is used in only one place.

1. Can i store this email address in the code file where i am using?
2. Can i have one common code file to have all the constants used by the application?
3. Can i have a table called 'key_values' table which will store values in an array list format
eg: Key Value
-------------------------------
email_addr myname@mycomp.com
sender_role admin,user,guest

please let me know which option would be good.
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the three options you mention are all possibilities, for configuration options that rarely (if ever) change -like email addresses- I'd use a properties file, and make its contents available to all classes of an application. That could happen either through a static method in some class, or through a config object that each class has access to.
 
author & internet detective
Posts: 38921
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is for "operating systems, applications, hardware, drivers, features, etc. General computer use stuff." I'm going to move it to one of our coding forums for you.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ulf Dittmer states, best practice is to store these in a properties file for the stated scenario. They can as well be written in the sources, but that leads to snarled situations if the project becomes commercially scaled, use this on for prototyping.


  • 1. Can i store this email address in the code file where i am using? Yes for protoyping only.
  • 2. Can i have one common code file to have all the constants used by the application?

  • That is a properties file.
  • 3. Can i have a table called 'key_values' table which will store

  • Yes, but where do you store the <K,V> between invocations of the program ?

    values in an array list formatting:
    Key Value


    That is a Map, not a List.
    [ February 03, 2008: Message edited by: Nicholas Jordan ]
     
    Rancher
    Posts: 4686
    7
    Linux Mac OS X VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    as others have said, for small scale, protoypes, etc. it doesn't matter.

    Once you get serious, the usual pattern is to put serious constants in a property file. When (not if) you find you have lots of properties,
    you put the real properties in a database table in a suitable database, and put the pointers to the database (i.e. the database, connection url, etc.) in a properties file.

    Read the properties, find the database, use JDBC to talk to it, get the real values.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!