1

There are a few immutable variables to be set and used throughout the entire project. Is it correct if I simple have a class with a couple of static fields in it that I can call? Is there a better approach?

The context includes both "regular" Java projects (e.g. web service kind of thing) and Android projects. Example of what I am doing at the moment:

class Settings {
    public static final String APP_TAG = "Some name";
    public static final String URI = "http://43.12.323.12:8080/Server";
    public static final int MAX_NUMBER_OF_PIGEONS = 25;
}

Would loading some proj_name.config be that much better? As of now, I see this alternative as extra work and no benefits.

0

2 Answers 2

3

Yes, it is fine to consolidate "global constants" into a single object like you are doing, but you may want to consider placing all these constants in an XML resource, which allows greater flexibility in customization, localization, etc.

Note that if you elect to go the XML resource file, you will have to pass a context to any consumer of these constants.

Sign up to request clarification or add additional context in comments.

Comments

2

You have at least two possible ways of doing this in Android.

  1. The use of SharedPreferences. This way you can save all the information you need in memory. Another

  2. The resource files that are created in your Android project (such as Strings.xml) could help you aswell to keep a some information in a static way in your app.

3 Comments

The context includes both "regular" Java projects (e.g. web service kind of thing) - this would be a problem with both of your suggestions.
@vikram "Regular" Java projects can use XML resources, it's only the SharedPreferences that's very Android-specific. So only the first suggestion is problematic.
@scatmoi Wouldn't "regular" Java projects (e.g. web service kind of thing) be running independent of the android project? Yes, you can include the strings.xml file in both sub-projects, but then, calling them global settings would not be correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.