1 2 Previous Next 17 Replies Latest reply: Apr 8, 2009 2:17 PM by jschellSomeoneStoleMyAlias Go to original post RSS
      • 15. Re: Static Vs Non-static methods
        796440
        Juraj wrote:
        jverd wrote:
        Juraj wrote:
        I have made a test and the non-static methods are faster than static
        It's unlikely that that's true to any significant degree. Most likely your test is flawed. In addition, you do NOT choose static vs. non-static for performance reasons.

        EDIT: In fact, if there were a measurable difference, I'd expect static method invocation to be slightly faster, since it's compile-time bound.

        Edited by: jverd on Apr 8, 2009 8:47 AM
        Yes I choose static vs. non-static for performance reason
        No, you don't. At least not if you know what you're doing.
        • 16. Re: Static Vs Non-static methods
          jschellSomeoneStoleMyAlias
          BigDaddyLoveHandles wrote:
          deepak_1your.com wrote:
          Sorry mate... did not get that.
          By definition a utility class has all static methods. So as soon as you mention "utility class" you've already made a decision. The real question is "should method X be static or non-static"? My default position is to assume no method should be static, and then wait to be convinced.

          Look at utility class java.lang.Math, for example. It has static methods sin(), cos(), sqrt(), etc... An obvious choice for a utility class, right? Then they introduced class StrictMath in 1.3 with exactly the same static method signatures. That's a code smell that one should have written an interface and implemented it in at least two ways, but it's too late for that because the original methods are static.
          Hindsight is a great thing.

          However I don't want to write code that is infinitely maintainable. Given that requirement then every single class, including inner private ones should have an interface.

          What about factories? Shouldn't every class have a factory just in case that need arises in the future?

          Myself I don't want to write code like that. And I most definitely don't want to maintain it.
          • 17. Re: Static Vs Non-static methods
            jschellSomeoneStoleMyAlias
            Juraj wrote:
            Yes I choose static vs. non-static for performance reason
            Presumably after automated profiling the entire application under a load that mimics a real production load.

            If any part of that is not true then you did it wrong.
            1 2 Previous Next