1

So the constants in my Java Application will be changed by the user since they specify local database requirements (username, password, port Number etc.). Now should I just keep them in my main class and specify to the user that they will have to change the following variables in this class. Or should I define them in an interface that my main class will implement ? Or any better suggestions ?

Edit: Oops I realized I have used incorrect terminology. I should have used setting variables instead.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Aerole
  • 105
  • 2
  • 10
  • 4
    I'm a bit confused - constants aren't supposed to change – Krease Aug 05 '15 at 00:13
  • 2
    *constants in my Java Application will be changed* Wut ? – Shreyas Chavan Aug 05 '15 at 00:15
  • 1
    I think you mean configuration. On Windows, you want to store them in `{user.home}/AppData/{Local}/{Roaming}/{Application name}`, Mac OS is something more like `{user.home}/Library/Application Support/{Application name}`. There are numerous options, have a look at [this discussion](http://stackoverflow.com/questions/19556932/how-to-save-the-state-of-my-minesweeper-game-and-then-load-it/19557052#19557052) for more details – MadProgrammer Aug 05 '15 at 00:16
  • Sorry for the confusion, I added a edit note. Should of been setting/configuration variables. – Aerole Aug 05 '15 at 00:43

4 Answers4

2

Values that remain constant during the program's execution, but can be changed, are called settings, or preferences. Java supplies preferences API, which can be used for this purpose.

Here is an example of retrieving user preferences using this API:

Preferences  prefs = Preferences.userRoot().node(this.getClass().getName());
String userName = prefs.get("userName");
String encryptedPassword = prefs.get("encryptedPassword");
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hey if I were to use this approach would you advise creating a separate class to write all my preferences. Then I can tell the user to edit these fields in this class. – Aerole Aug 05 '15 at 03:36
2

I think I understand what you intend to do. What many people do is create a properties file that is read in by your java program which treats those variables as values that don't change.

This enables the user to change the config file before execution and allows your program to treat those variables as non-changing. I'm not sure I'd make them constant, but they can be treated as non-changing values.

http://javarevisited.blogspot.com/2012/03/how-to-read-properties-file-in-java-xml.html

Android XML layout constants

Community
  • 1
  • 1
Pumphouse
  • 2,013
  • 17
  • 26
  • 1
    I think this is exactly what I'm looking for. Thank you. Only question is where should I typically place the properties file within the project hierarchy. Should it be package level or within the package that uses it ? – Aerole Aug 05 '15 at 00:45
  • @Aerole that totally depends on the design of your application. Put it in whatever directory it makes the most sense for a user who is not you to look – Pumphouse Aug 05 '15 at 00:48
0

I think that the location of these variables depends on how you choose to implement your program design. It takes practice to learn when it makes sense to create a separate class, so don't get frustrated if you don't understand at first. If your main class is getting lengthy and complicated, that is one example of when it is probably time to split things up into separate classes.

You may find these links helpful for deciding when it is appropriate to create separate classes:

http://stackoverflow.com/questions/18806699/when-to-create-helper-methods-and-separate-files

https://softwareengineering.stackexchange.com/questions/154228/why-is-it-good-to-split-a-program-into-multiple-classes

If you are trying to save variables more permanently (i.e. through a device restart, through a device power off) you can use preferences. Replace dataType in these methods below with String, char, int, etc.

  //save prefs
    public void savePrefs(dataType key, dataType value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putDataType(key, value);
        editor.commit();
    }

    //get prefs
    private String LoadPreferences(dataType key, dataType value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        DataType data = sharedPreferences.getDataType (key, value);
        return data;
    }
Community
  • 1
  • 1
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
0

We in our current project use a excel sheet to map a property and its value. The users can modify the excel sheet with want value the want and send it to us.

We generate an XML from the excel data. we have created a 'configuration' page where we upload the XML. And we have written a program that will run every 30 mins to check if the data in the XML has changed. Not the best way to do things but it works for us as we have lot of properties in our app that we need to keep changing as per business needs.

Albert Pinto
  • 392
  • 2
  • 6
  • 17