In my project I am developing at the moment, I have many configuration settings. Things such as
- Alarm times
- Amount of items to retrieve from the server
- LocationManager integers such as minium location
These are all static final and are all in a class that corresponds to the value.
My question is, are there any problems with moving all of these values to a single static class?
My thinking is that when it comes to testing and tweeking the app, it will be easier to manage.
Building on @Snicolas’s answer…
You should indeed persist your CONFIGURATION settings outside of code (file or database). BUT you should not “read” that configuration each time a value is required, that would be inefficient.
Using a class to manage configuration (ie. AppSettings) is a good idea. Making it static is one way to provide singleton-like access. In C# and ASP.NET a web app will guarantee one and only one instance of a static class and therefore multiple un-related requests from different users will share the exact same static values.
But in your case (I see the tag ‘android’) using Java your best bet may be a Singleton approach. I don’t know how garbage collection works in Java but I’d say you should use a singleton to ensure one-and-only-one instance of your settings. The singleton Ensures an instance exists (or creates one if not) and then provides it to the caller.
This may also make it easier to support the ability to change configuration values while the app is running — you can “watch” for setting changes on a regular basis.
I’m not a Java man but I’d be surprised (well no not really) if there wasn’t already a library for handling this very problem.