简体   繁体   中英

Instance vs static in making a blackbox method in android

My question is regarding in creating a method to be used in my android app. This method is a blackbox that accepts string as a parameter then converts the string object into something.

I want to call this method everytime a certain button is clicked (from a fragment and activity) and I'm wondering if static is the better approach for this since i dont need to instantiate.

I read the answer in Difference between Static methods and Instance methods and says that static is better but i found a blog that one should avoid static as possible.

So what's better approach to implement for my blackbox method?

If you place your method in activity class which contains button with onClickListener calls this method, there is no need to make it static, because the instance of activity created anyway.

If you use a separate class with utils methods, then it may be a good idea to use a static method.

This article is simple explanation of rules to make a method static in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM