1

Are there any differences between the two approaches in terms of the lifecycle of these variables?

As I understand in both cases foo gets garbage collected when the app get's cleared from the memory at the very

end. (correct? or am I missing something?)

Option 1: Storing a static variable in the Application class:

public class App extends android.app.Application {
    private static SomeClass foo;

    public static init setSomeObject(SomeClass someValue){
       foo = someValue;
    }


    public static SomeClass getSomeObject(){
       return foo;
    }
}

Option 2: String a static variable in any static class?

public class JustSomeClass {
    private static SomeClass foo;

        public static init setSomeObject(SomeClass someValue){
           foo = someValue;
        }

        public static SomeClass getSomeObject(){
           return foo;
        }
}

I prefer the second, purely for better readability as I don't want my Application class to grow too big, but I need to know if there are any disadvantages with that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Crocodile
  • 5,724
  • 11
  • 41
  • 67

2 Answers2

2

Conceptually, static is an abnormality in good OO design. So when you ask about best practices, start by thinking up designs that do not use static in the first place.

And beyond that: your classes should resemble a helpful model of the "thing" you intend to built. You don't put "stuff here ore there"; you put it ... where it belongs to from a conceptual point.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

I agree to GhostCat and Chetan. I too feel using static must be avoided to avoid excess of memory usage.

I'd prefer making a singleton class for each process, say, to handle flow of screens in my app or to write the helper methods for database operations or few common methods used by many activities/ fragments in my app.

This link has a good discussion on Application class vs. Singleton.

Community
  • 1
  • 1
Aishwarya Tiwari
  • 625
  • 4
  • 19
  • So putting aside the OO design persective the variables in both cases will not be garbage collected until the very end? Regarding the design: my idea was to create global helper classes like SomeClass for specific roles (e.g.: general db methods) which can be reused from various Activities/Services rather than cram everything in one class extending the Application class. Reading the link you sent... I might also consider singletons. Thanks! – Crocodile Mar 01 '17 at 07:57
  • @Crocodile I'm unable to post the answer to the question you asked in the comment above. I'm not getting why stackoverflow is giving error. Any idea? – Aishwarya Tiwari Mar 01 '17 at 10:06
  • 1
    @Crocodile- 2) Yes, putting everything in the application class will just increase the clutter as your app size grows. 3) You're welcome! :) – Aishwarya Tiwari Mar 01 '17 at 10:10
  • 1
    1) As static variables are class variables hence, yes, in both cases your static variables won't be garbage collected until the class is garbage collected which in turn will happen when the class loader is garbage collected in JVM. In Android this will happen only when your application is killed i.e. application crashes, you force stop it, Android kills it for memory concerns (that doesn't include only exiting the application, since it is still in the recent application list). – Aishwarya Tiwari Mar 01 '17 at 10:18