Should I use Constructor in a single method class or just static method
I have a method which I’m using in multiple activities, so I’ve put it in separate class. That class only has that single method which I made to be public static. Should I instead create a constructor and call that method in my activities like that? eg.
MyMethodClass.myMethod(context, arg1, arg2);
or
MyMethodClass a = new MyMethodClass(context); a.myMethod(arg1, arg2);
What is the difference in terms of memory/cpu usage and response times?
If you have a common method that you use in all of your activities, just create a BaseActivity
and make your other activities inherit that BaseActivity
. Make sure your BaseActivity
inherits AppCompatActivity
too.
I think the "proper" way would be the first one you have shown.
And while both approaches work, and while I agree with @a_local_nobody (see his comment under your question) that it’s based on opinion, you can avoid unnecessary code (constructor call or instantiation) and unnecessary use of ressources (you don’t create a class instance) with the first approach.
So it definitely has tangible (if probably negligible) advantages.