To for Java?

Never really liked to make the code in static methods, doing it usually, if he does without it. And here thinking and I can't find the correct answer to, it seems to me, quite an easy question.


Does it make sense (for resources, or performance ?) in the transformation:

public class A
{
 private final int a;
 private final Map<integer, integer> map;

 public A(int a)
{
 this.a = a;
 this.map = new HashMap<integer, integer>();
}

 public int calcSomthing()
{
 int b = 0;

 for (int i : map.values())
 if (i == a)
b++;

 return b;
}
}</integer></integer,>



in

public class A
{
 // ...

 public int getA()
{
 return a;
}

 public Collection<integer> getValues()
{
 return map.values();
}

 public static int calcSomthing(A obj)
{
 int b = 0;

 for (int i : obj.getValues())
 if (i == obj.getA())
b++;

 return b;
}
}</integer>



If we consider that objects a lot, say 50 thousand.


Or washed away will only if you have to make those methods static, which did not turn directly to class variables, and use only the other class methods?
October 14th 19 at 11:36
5 answers
October 14th 19 at 11:38
Solution
Answer: no, such changes to do not worth it. Generated JIT code for both options is almost the same, but the first option looks cleaner and ideologically correct. Do not bother the fact that calling a static method is supposedly faster virtual. In fact, due to the dynamic devirtualization JVM can call the method directly, bypassing the vtable. Moreover, in some cases, a call to a static method may be even worse due to the class initialization barrier. In General, write as is better from the point of view of architecture and OOP concepts.
October 14th 19 at 11:40
readability and code Preobrazhensky important benefits in several commands
October 14th 19 at 11:42
I seriously doubt that there's any difference in principle. Pass the object explicitly as a parameter or implicitly as this, the performance is not affected.

For 100% guarantee it is necessary to conduct an investigative experiment or arm disassembler.
No wait, you're in the second case, instead of fields using the getters. So the second option will be even slower. - Naomie.Volkman51 commented on October 14th 19 at 11:45
And in a situation when a method does not access variables, and uses other methods within the class? - elias.Waelchi3 commented on October 14th 19 at 11:48
Then I see where will the difference in performance - Naomie.Volkman51 commented on October 14th 19 at 11:51
I'm very confused already :)
Just stumbled across someone else's code where a lot of techniques handed down in static — wondered why. - elias.Waelchi3 commented on October 14th 19 at 11:54
October 14th 19 at 11:44
Thought about it and came to the conclusion that I'm wrong. The difference may be, because if the method is virtual, the possible polymorphism, i.e. we need to find which method to call. In the case of a static method, all known at compile time, so the extra runtime processing is not necessary. I.e. in the General case, other things being equal, a static method will be faster.
In theory, we should remember that suggest about pre-optimization ;) And if you need to know for sure — check experimentally. - Naomie.Volkman51 commented on October 14th 19 at 11:47
Now just write a couple of tests, became curious. - elias.Waelchi3 commented on October 14th 19 at 11:50
October 14th 19 at 11:46
Here is another interesting read
The basic idea: static faster than private or public.

Find more questions by tags JavaOOP