Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 lifetime of application.

I have been reading lot of how to share object between activities and I think the best will be to create one static class. What do you think? Should I use another approach? I am asking because I have read lot of counter opinions over the internet.

Thank you.

like image 926
Mihalko Avatar asked Feb 26 '13 07:02

Mihalko


People also ask

Why do we need static class in Java?

In Java, the static keyword is primarily used for memory management. We can use the static keyword with variables, methods, blocks, and classes. Using the static class is a way of grouping classes together. It is also used to access the primitive member of the enclosing class through the object reference.

Why You Should Avoid static classes?

Static classes have several limitations compared to non-static ones: A static class cannot be inherited from another class. A static class cannot be a base class for another static or non-static class. Static classes do not support virtual methods.

Why would you use a static class?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

What is static class in Android?

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.


3 Answers

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();
like image 68
Elias Mårtenson Avatar answered Oct 19 '22 07:10

Elias Mårtenson


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.

like image 23
Gabe Sechan Avatar answered Oct 19 '22 07:10

Gabe Sechan


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.

like image 28
Bondax Avatar answered Oct 19 '22 06:10

Bondax