Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to keep data in static variables?

In an Android application, is it bad practice to store objects in static fields in these cases?

  1. Application data. Is it bad to keep application data in static variables in a class while the application is running? Currently, I'm storing the data in an instance variable in my Application class. Then classes that need the data can obtain the data from the Application.
  2. Context's etc. Is it bad practice to store a Context (e.g. a reference to an Activity or an Application) in a static field? This can be used in a class that needs e.g. a LayoutInflater or resources. Currently, I am passing the Contexts to methods needing them as arguments.
like image 705
PurkkaKoodari Avatar asked Nov 04 '13 18:11

PurkkaKoodari


People also ask

Is using static variables bad practice?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.

What is the disadvantage of static method?

in static method we cannot call normal functions that is non - static functions. The static method can not use non static data member or call non-static method directly. this and super cannot be used in static context. Access only static type data (static type instance variable).

Should you avoid static classes?

There is nothing wrong when static class that is used by different part of the application does not have a state and always return deterministic results. This class is stateless, and its output is always predictable. The Math class can be used directly in many parts of the application without the risk of side effects.

Are static global variables bad?

The biggest problem with a global variable is that it is global, so anyone can access and modify it at any time. That problem is immensely reduced with a static variable, because all code accessing and modifying the variable is in that one file, and hopefully under your control.


1 Answers

Yes and Yes. :)

Static Fields. There are a lot of problems with excessive usage of Static fields. Not only they are slower to access by an interesting margin, but also they are prone to be destroyed overnight by Android, and it's usually hacky to check for their references all over the place or fill your getter/setters with if (sSomeStatic == null) { return new SomeStatic()}. It's ok to store a static reference to a class called (for example) ApplicationData where you store some values, hey, we NEED some globals every now and then, but it's so easy to abuse it, that I frown every time I inspect new Android devs' source code.

Yes, store your Application instance in a singleton pattern and use it, but don't add 200 static fields to your Application implementation just because you can do YOURAPP.getInstance().SomeLazyValueYouAddedHere();

That is bad. It leads to bad practices and it will be slower than having a good design where you access hard references.

I could go on forever but there are a lot of StackOverflow discussions (some heated!) about this. If you are here, I'm assuming you're asking for experience; I've been doing Android for a couple of years in different projects and my experience has always been that the less Static, the merrier.

Now the context… oh the Context. Don't store the Context in a hard reference, ever. Or you will leak memory. An activity has references to View and a multitude of other things. If you store the Context, you're storing the activity and things go bad from there. Learn to pass the Context around, use the Application Context whenever possible and if you need to pass it around, do it for very good reasons. Most of the time the App context is enough for getting resources, strings, etc. If you're going to store the Context, always store context.getApplicationContext(); Never store a static activity context. You can google this too and StackOverflow has some good answers.

If you can afford one and only one Android book, get the BNR one. Even though Android may release new SDKs every now and then, the concepts are completely valid and the patterns the author uses are the right way to deal with Activities, Contexts, Fragments, etc.

UPDATE Your Application should look like this:

public class YourApp extends Application {
   private static YourApp sInstance;
   public YourApp() {
      super();
      sInstance = this;
   }
   public static YourApp getInstance() {
      return sInstance;
   }
}

And in that case, yes, you are getting the same Static reference to the same App Context.

like image 122
Martin Marconcini Avatar answered Oct 22 '22 00:10

Martin Marconcini