Static class in Java (Android) - use or not use

后端 未结 3 713
小鲜肉
小鲜肉 2021-02-08 12:38

Recently I have started development in Java for Android.

My idea is to create one static class which will load ton of stuff on the beginning and store results for a life

相关标签:
3条回答
  • 2021-02-08 12:47

    The problem with your solution is that you're basically creating a huge stack of globals. It's sometimes unavoidable, but it has the same type of problems globals always have- you quickly end up with hard to read code that doesn't really have a good OO breakdown. You can use this, but use it sparingly- only with important data structures that are really going to be shared between many activities.

    0 讨论(0)
  • 2021-02-08 12:59

    I'm assuming that you were referring to static fields of a class, as opposed to static class which, as Wyzard pointed out, is something completely different. As a general rule of thumb, holding information in static fields is not a good idea in Java. The reason for this is that it prevents the ability to instantiate multiple instances of whatever it is you store in the class.

    In the specific case of an Android application, the best way to deal with the issue of having data stored associated with the application itself is to subclass the android.app.Application class and use it to handle application-global data:

    class FooApplication extends Application
    {
        private String privData;
    
        public String getPrivData() {
            return privData;
        }
    }
    

    You then need to declare that this class is your main application class (instead of the default Application). In the application entry in AndroidManifest.xml add the following:

    <application android:name="com.example.application.FooApplication"
                 ...>
        ...
    </application>
    

    You can then look up the application instance from anywhere inside your application using the method Context.getApplicationContext() which will be an instance of your Application subclass:

    FooApplication app = (FooApplication)Context.getApplicationContext();
    String privData = app.getPrivData();
    

    Depending on from where you are trying to look for subclass of "Application", you may have to invoke the "getApplicationContext()" without "Context":

    FooApplication app = (FooApplication)getApplicationContext();
    String privData = app.getPrivData();
    
    0 讨论(0)
  • 2021-02-08 13:14

    Android provides a class called Application, which is will not be gc'ed as long as your Application isn't killed. Use this class for initialization, static classes as containers are somewhat ugly, but i can't pinpoint why that is.

    I only use them as containers for constants such as bitmasks which can't be expressed as EnumSets.

    As the other posts mention SharedPreferences: I think the preferences exist to store values, but not to load your structures that you need for you application. These structures should be loaded from a construct that represent or make up a model for your data's semantics.

    0 讨论(0)
提交回复
热议问题