Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Voxera113888yIf it is shared data that all users have the same and if it can be recreated if necessary its just another form of caching.
I use it in c# for some things that do not serialize well and that need to be machine local.
If it contains any user data your walking a very slack barbwire line that will draw blood. -
ste0910398yIn general, using a static field is not bad "per se". It depends on what you do with it. If the purpose is to create shortcuts and pass data between different classes (pages in your case) then I would look to different approaches (messages, DI ..)
-
Voxera113888y@jetttlag depending on platform, statics are shared by all visitors which can make for an interesting mix of information.
I have seen it happen where page settings was stored in statics and several users tried to change them.
The result was very random.
Luckily it was no secret information that leaked. -
senior android dev here! (but still, I may be wrong, take this with a grain of salt)
I think you are right, generally it's not a good practice for a couple of reasons:
- doing so tight-couples the class that has the static variable with any classes that use the variable
- doing so makes it's hard to reason about the outcome of the code that use the variable since the variable could be any value
I suggest going with event bus kind of thing if you want to synchronize state between all component like a bunch of fragments with a bunch of activities
Related Rants
Is storing-of-data-in-classes-as-static-variables-to-access-them-later-on-from-one-page-to-another a good practice? It always seemed fishy to me...
undefined
android
java