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?

Add Comment
2 Answer(s)

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.

Answered on July 16, 2020.
Add Comment

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.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.