Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will static variable live even after application gets closed?

Consider my application has one activity and I have a static variable in that activity. At first time i initialize the variable , starting a service and exit from the application. At some instance am trying to access the static variable which i initialized before. Sometimes the value is present. but at some times null value is retrieved. plz advise whats going wrong

Thanks in advance.

like image 436
Prasath Avatar asked Feb 25 '23 09:02

Prasath


2 Answers

Once the activity is killed by the SO, all the memory resources are back to the system, so you lose the data in that variable. If the activity goes into background instead, the value will be retained

like image 147
Aleadam Avatar answered Mar 05 '23 07:03

Aleadam


You cannot rely on a static variable to persist indefinitely. When your application terminates, your statics are gone. If you need to store a value permanently, persist it to a database, filesystem or some other means of storage.

like image 28
Asaph Avatar answered Mar 05 '23 08:03

Asaph