0

I just want to see what you guys might think the difference (in terms of usage, efficiency, or even good practice) with regard to android development.

If I use a static variable in one of my base activity (so its single instance and be access everywhere), as opposed to using a non static variable in my application subclass (which is a single application class for all the activities).

Both will achieve the same end results if you try to use a global variable.

I was using the static one then I moved to use the application subclass (in case you guys wonder "what is it I am using for", I wanted to play background music and control it from everywhere and I don't wish to use service for certain reasons).

Any help clarifying the best way to ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    I think you might want to read http://stackoverflow.com/questions/2475978/using-static-variables-in-android - it answers your question in my opinion. If you are using just one static variable, it's probably less overhead than a singleton class, but really, in the bigger scope of things, it makes no real difference and static variables exist for a reason - they are not bad practice if used sparingly. – Ewald Jun 01 '12 at 04:49
  • Thanks but this answers whether I should use static or not. My question is whether to use static or application subclass and what would be the differnence as both are preserved in the memory – Snake Jun 01 '12 at 04:52

2 Answers2

3

It depends on use also, suppose if you are using

android:process

for some reason in your Activity or anything else in your Manifest file your static value will be reset and you will get the initial value assigned to static variable. In that case you can use SharedPreference or Application class.

Because if you use android:process for any particular Activity then that Activity will run in another process and as we know that in Android every Application runs in its own process.

Other than this I don't see there is much issue using static. But, personally I would prefer Application class as Android has it for global variables.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • The [life cycle](http://www.javaying.com/2007/01/whats-lifetime-of-variables.html) of static variable is through program's life cycle, so static value will never get reset. – Lucifer Jun 01 '12 at 05:11
  • 1
    @Lucifer Android create a seperate process when we use `android:process` so it a seperate it will reset its value. – Lalit Poptani Jun 01 '12 at 05:14
  • Agree that Dlavik Virtual Machine creates separates process for each application. but I don't think static changes its behavior in any programming language as it name stats. do you have any doc link related to this ? – Lucifer Jun 01 '12 at 05:17
  • please visit [this](http://stackoverflow.com/a/5306067/996493) and [this](http://stackoverflow.com/a/1944564/996493) links – Lucifer Jun 01 '12 at 05:19
  • 1
    @Lucifer it might not be mentioned in the docs but you can try it. – Lalit Poptani Jun 01 '12 at 05:22
  • 1
    This could be an important feature of static variable in android, strange that it is not mentioned in the doc. anyways thanks for such nice information :) – Lucifer Jun 01 '12 at 05:34
  • Thank you for the confirmation of using Application class – Snake Jun 01 '12 at 15:05
1

During the execution of a program, each variable has its own time within which it can be accessed. This is called the lifetime of the variable.

  • Instance variables: Instance variables are class members. Every time you create an object from a class, a brand new copy of these instance variables is created for this object. Actually, creating an object from a class means, partially, creating copies of instance variables for that object. So each object has its own copy of instance variables which exist as long as the object they belong to exists. The values of these variables constitute what we call: the state of the object.

  • Static variables: Static variables are also members of a class but can't belong to any object created from that class. So created objects from the class don't get their own copies of static variables. Therefore static variables are created only when the class is loaded at runtime. The existence of static variables is dependent only on the class itself. As a result, a static variable exists as long as its class exists.

One of main difference between these two variable is that when you are calling System.gc(); your instance variable is set to null while static variable will never set to null by calling gc.

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • Your information is more useful dear Lucifer. Can i know more details in your chatroom if you give permission to enter your chatroom? – Loquatious Jun 02 '12 at 12:24