I have an application with 100+ static variables and methods in its Application class will it affect the performance of the app? if so how to handle it?
Asked
Active
Viewed 1,153 times
2
-
4maybe your design is a bit wrong though – Scary Wombat Nov 22 '16 at 06:03
-
any alternate way to handle it? – Vignesh Palanichamy Nov 22 '16 at 06:04
-
use preferences keeping static variable consumes memory as those are alive untill you class or in this case application is active. – Bali Nov 22 '16 at 06:05
-
1Create a helper class and put them in it! – A. Badakhshan Nov 22 '16 at 06:06
-
1try this http://stackoverflow.com/questions/6196027/need-of-static-variables-and-their-overhead-on-jvm – Pavneet_Singh Nov 22 '16 at 06:07
-
@shadoWalker that would not be any better. The main problem with static variables in Android is that the OS can throw the app out of memory when it is in background and the memory is otherwise needed. When the app is then restarted all the values are lost. – Henry Nov 22 '16 at 06:09
-
1@Henry, I don't think that he is trying to save his data in static variables. I think his wants to access them everywhere in his app. – A. Badakhshan Nov 22 '16 at 06:11
-
what if i move all the variables and methods to a normal class and access it with singleton objects. it makes any difference by using application class and normal class. – Vignesh Palanichamy Nov 22 '16 at 06:13
-
@shadoWalker The problem still remains, see the activity life cycle: https://developer.android.com/reference/android/app/Activity.html and in particular the method `onSaveInstanceState` https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) – Henry Nov 22 '16 at 06:18
-
i will remove the static access to all and access it with objects.is that works? – Vignesh Palanichamy Nov 22 '16 at 06:23
-
Creating instances and make an object singleton, has their own definition and are used in right places. And from where I see, you don't need them. – A. Badakhshan Nov 22 '16 at 06:27
-
Too much static variables? Then that is bad implementation. Reduce the use of static, and when it is absolutely necessary use static – Akshay Bhat 'AB' Nov 22 '16 at 06:39
1 Answers
1
As I see, your only question is if using static
methods and variables inside your application class is wrong or not. As far as I know, it will not affect your application performance or anything else, but putting them inside application class is a wrong design. It's like you put all your code inside one single method and it will not be good.
So, I suggest you to put your static
methods inside a Helper
class and since they don't need to be instantiated, they need to be static
, not a singleton
. Also for your variables, for example for your String
s, I suggest to create another class and put them into it so your application maintenance be possible.

A. Badakhshan
- 1,045
- 7
- 22
-
-
See for example this question http://stackoverflow.com/questions/4797187/android-static-variable-null-on-low-memory why this is not a good idea. – Henry Nov 22 '16 at 06:32
-
Guys his question is not what you are answering. I agree with both of you, but his question is another thing. – A. Badakhshan Nov 22 '16 at 06:34