For Java lovers?

Asked bysadeghi 1363

I never really liked to make code in static methods, I did it normally, if I could not do it without it. And then I thought about it and still can’t find the right answer to, it seems to me, a rather easy question.

Does it make sense (by resources, speed, or?) In the conversion:

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;
    }
}


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;
    }
}


Given that a lot of objects, say 50 thousand.

Or it will only be washed if, in static, those methods that are not initially addressed directly to class variables, but use only other methods of the class?

Answers

tiana
The answer is: no, you should not deal with such transformations. The code generated by the JIT compiler for both options will be almost the same, but the first option looks clearer and ideologically more correct. Do not bother with the fact that the call to the static method is supposedly faster than the virtual one. In fact, due to dynamic devirtualization, the JVM can call the method directly, bypassing the vtable. Moreover, in some cases, calling a static method can be even worse due to the class initialization barrier. In general, write as best as possible in terms of architecture and OOP concepts.
jacqueline treiber
readability and convertibility of the code is more important than the benefits in several commands
jacob mcconnell
I strongly doubt that there will be any difference in principle. The object is passed explicitly as a parameter or implicitly as this does not affect performance.

For a 100% guarantee, you must conduct an investigative experiment or arm yourself with a disassembler.
Replies:
Although stop, you are using getters instead of fields in the second case. So the second option will also be slower. - misery cordia
And in a situation where the method does not refer to variables, but uses other methods inside the class? - vivek srinivasan
Then I don’t see where the difference in performance will come from - crowinator
I’ve got myself confused already :)
Just came across someone else's code, where many methods are rendered in static - I wondered why. - janet coulthart
bob simon
I thought and came to the conclusion that I was wrong. The difference may be because if the method is virtual, then polymorphism is possible, i.e. need to find which method to call. In the case of a static method, everything is known already at the compilation stage, so additional processing is not necessary. Those. in general, other things being equal, the static method will be faster.
Replies:
In addition to the whole theory, we must also remember what is advised about the preliminary optimization;) And if you need to know for sure, check experimentally. - andrew ramler
Now I’m just writing a couple of tests, it was curious. - angie hanson
elim suleymanli
Here it is still interesting link
The basic idea: static or private is faster than public.
JavaScript: explain return in the constructor :: English teaser networks? :: Smart Hex Editor, preferably free? :: Are there any ready-made solutions for building a tree of dependencies between files? :: Ubuntu Enterprise Cloud
Leave Repply forFor Java lovers?
Useful Links