1 2 Previous Next 17 Replies Latest reply: Apr 8, 2009 2:19 PM by jschellSomeoneStoleMyAlias RSS

    Static Vs Non-static methods

    807588
      Hello,

      I wonder what should I use. I have got a class which does a lot of counting.. I can put the methods inside the class or make a class Math3D with the static methods that will count it.

      Which is the better way?
        • 1. Re: Static Vs Non-static methods
          807588
          Usually you will find that most utility classes have static methods and a private constructor.
          • 2. Re: Static Vs Non-static methods
            807588
            Adding to 'binary_coder' reply...

            Utility methods that does not require access to instance variables can be declared static. i.e. If the variables that the method uses internally are either method parameters or constants then the method and the constants can be declared static.

            Hope this helps

            -------

            deepak@1your.com
            • 3. Re: Static Vs Non-static methods
              807588
              As soon as you write "utility class" you are begging the question.
              • 4. Re: Static Vs Non-static methods
                807588
                Sorry mate... did not get that.
                • 5. Re: Static Vs Non-static methods
                  807588
                  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.
                  • 6. Re: Static Vs Non-static methods
                    807588
                    BigDaddyLoveHandles wrote:
                    deepak_1your.com wrote:
                    Sorry mate... did not get that.
                    By definition a utility class has all static methods
                    Where's this "definition", then?
                    class SomeUtilities {
                    
                      static void writeToFile(Something something) {
                          // mneh
                       }
                    
                    }
                    
                    class SomethingElse {
                    
                       void work() {
                          SomeUtilities.writeToFile(myPayload);
                       }
                    
                    }
                    Erk. In my unit tests, I don't actually want to write anything to disk. Now what? (stick an interface in the way of course, but you see my point)
                    • 7. Re: Static Vs Non-static methods
                      807588
                      George, I think we're agreeing, eh? The point is that static methods make testing harder because they can't be directly mocked, right?
                      • 8. Re: Static Vs Non-static methods
                        807588
                        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.
                        Good post.
                        • 9. Re: Static Vs Non-static methods
                          807588
                          Thank you for help, so I will use the non-static methods. I have made a test and the non-static methods are faster than static
                          • 10. Re: Static Vs Non-static methods
                            796440
                            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
                            • 11. Re: Static Vs Non-static methods
                              807588
                              BigDaddyLoveHandles wrote:
                              George, I think we're agreeing, eh? The point is that static methods make testing harder because they can't be directly mocked, right?
                              Aye
                              • 12. Re: Static Vs Non-static methods
                                807588
                                Juraj wrote:
                                Thank you for help, so I will use the non-static methods. I have made a test and the non-static methods are faster than static
                                :headdesk:
                                • 13. Re: Static Vs Non-static methods
                                  807588
                                  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
                                  • 14. Re: Static Vs Non-static methods
                                    JoachimSauer
                                    Juraj wrote:
                                    Yes I choose static vs. non-static for performance reason
                                    You did, but what jverd tried to tell you is that it's a very bad idea to make that decision for performance reasons.

                                    There are much more important aspects in that decision than performance.
                                    1 2 Previous Next