Solving static methods versus instance methods in java?

According to my understanding, we should look for instance methods only when they deal with the state variable of an object ie If a method deals with the state of an object, it should always be declared as methods of the ie static class. But still in most projects i have seen methods that never work with instance variables, they are also declared as instance methods (basically what these methods do, they use some method parameters and do some processing of these parameters and call some other classes ) This, Should these methods be declared as methods of the ie static class?

+4
source share
5 answers

Probably the answer is yes. If you have an instance method that does not actually take advantage of the state of the instance, then it should probably be static and possibly be ported to a helper class depending on what it does.

Note that even if you do not gain access to instance variables, access to instance methods will also disqualify the method from static . In addition, if this method is an instance method to ensure its future (in anticipation of using the state of the instance later), then changing it is also impractical.

It is also important that public non-static methods can be inherited and overridden by a subclass, so making them static can really break the code, possibly in unexpected ways.

+7
source

Here's a [possibly incomplete] list, when you should use instance methods on top of static:

  • you access instance variables / methods from a method
  • the method is an abstract method that you implement
  • method is an interface method that you implement
  • you have doubts that the method remains static in the long run
  • you declare it synchronized and don't want to block the class, not the instance
  • you get warnings when accessing static methods in a non-stationary way, and you really care about them (sometimes you just cannot avoid calling in a non-static way, so your only choice makes their methods non-static)

You will probably become static in all other cases.

+7
source

Static methods have the disadvantage that they tightly connect subscribers to the implementation. Instance methods can be overridden or can be one of several implementations of an interface method.

In other words, instance methods can promote free connectivity, testability, and reuse.

+3
source

You cannot expect everyone to follow the path all the time, whether it is best practice or not. First of all, we are all human. We can sometimes choose something else, and this does not have to be completely correct all the time. Even structures and libraries and languages โ€‹โ€‹are created by people, so a mistake should not surprise you or blind you.

For everything else, I agree with dlev.

+1
source

Suppose we are developing a new language and we want Sqrt to be an instance method. Therefore, we look at the double class and begin designing. Obviously, it has no input (except for the instance) and returns double. We write and test the code. Perfection.

But the square root of the integer is true, and we donโ€™t want to force everyone to convert to double in order to get the square root. So we go to int and start designing. What does he return? We could return an int and make it work only for perfect squares or round the result to the nearest int (ignoring discussions about the correct rounding method at the moment). But what if someone wants an incomplete result? Should we have two methods - one that returns int and one that returns double (which is impossible in some languages โ€‹โ€‹without changing the name). Therefore, we decide that he should return double. Now we are implementing. But the implementation is identical to the one we used for double. We copy and paste? We cast an instance in double and call this instance method? Why not put the logic in a library method that can be accessed from both classes. We will call the Math library and the Math.Sqrt function.

0
source

Source: https://habr.com/ru/post/1369207/


All Articles