1 2 Previous Next 19 Replies Latest reply: Mar 26, 2007 4:42 PM by 807606 RSS

    static final method

    807606
      Why would a programmer declare a static method as final? Speaking about class designing.
        • 1. Re: static final method
          JosAH
          Straight from the JLS:

          8.4.3.3 final Methods

          A method can be declared final to prevent subclasses from overriding
          or hiding it. It is a compile-time error to attempt to override or hide a final method.


          Oh, it's you again ...

          Jos
          • 2. Re: static final method
            807606
            Straight from the JLS:

            8.4.3.3 final Methods

            A method can be declared final to prevent
            subclasses from overriding
            or hiding it. It is a compile-time error to attempt
            to override or hide a final method.


            Oh, it's you again ...

            Jos
            My Goodness, let me ask in another way, detailing a bit more: why would a programmer want to hide a static method? No benefit doing it.
            • 3. Re: static final method
              807606
              My Goodness, let me ask in another way, detailing a
              bit more: why would a programmer want to hide a
              static method? No benefit doing it.
              My goodness...
              Hiding a static method is not a good thing.
              Declaring static methods as final prevents subclasses from hiding them.
              Soooo.... Declaring static methods as final prevents something that's not good.
              • 4. Re: static final method
                807606
                My Goodness, let me ask in another way, detailing
                a
                bit more: why would a programmer want to hide a
                static method? No benefit doing it.
                My goodness...
                Hiding a static method is not a good thing.
                Declaring static methods as final prevents subclasses
                from hiding them.
                Soooo.... Declaring static methods as final prevents
                something that's not good.
                My Goodness... if you "override" a static method in the subclass you have two static methods available. Sooooooooooooo... hiding a static method is a good thing sometimes, why not?
                • 5. Re: static final method
                  JosAH
                  hiding a static method is a good thing sometimes, why not?
                  Note the 'sometimes', when 'sometimes not' applies make your static
                  methods final.

                  Jos
                  • 6. Re: static final method
                    807606
                    hiding a static method is a good thing sometimes,
                    why not?

                    Note the 'sometimes', when 'sometimes not' applies
                    make your static
                    methods final.

                    Jos
                    In instance methods, overriding is a good thing. But in static methods, there isn�t a good reason for not permitting hiding the method.
                    • 7. Re: static final method
                      807606
                      in static methods, there isn�t a good reason for not permitting hiding the method.
                      See replies 4 and 5.

                      There either isn't a good reason for not permitting, or there is sometimes good
                      reason to permit. Make up your mind which you're asserting and stick to it.
                      • 8. Re: static final method
                        807606
                        hiding a static method is a good thing sometimes,
                        why not?

                        Note the 'sometimes', when 'sometimes not' applies
                        make your static
                        methods final.

                        Jos
                        You caught me. It seems that I am giving an argument, and at the same time the counter-argument for my argument. Let me rephrase: hiding the static method is a good thing sometimes, not hiding that static method is also a good thing sometimes. But not permitting the method be hidden is not a good thing, in any case.
                        • 9. Re: static final method
                          JosAH
                          Note the 'sometimes', when 'sometimes not' applies
                          make your static methods final.
                          Let me rephrase: hiding the static method is a good thing
                          sometimes, not hiding that static method is also a good thing
                          sometimes. But not permitting the method be hidden is not a good
                          thing, in any case.
                          Your conclusion if false because of your own reasoning: sometimes
                          it's good to hide static methods and sometimes it's not. Not permitting
                          methods to be hidden sometimes is a good thing when explicitly
                          applied (by using that final keyword).

                          Jos
                          • 10. Re: static final method
                            807606
                            Note the 'sometimes', when 'sometimes not'
                            applies
                            make your static methods final.
                            Let me rephrase: hiding the static method is a good
                            thing
                            sometimes, not hiding that static method is also a
                            good thing
                            sometimes. But not permitting the method be hidden
                            is not a good
                            thing, in any case.
                            Your conclusion if false because of your own
                            reasoning: sometimes
                            it's good to hide static methods and sometimes it's
                            not. Not permitting
                            methods to be hidden sometimes is a good thing when
                            explicitly
                            applied (by using that final keyword).

                            Jos
                            No.

                            ... let me try again:

                            You are subclassing a class. YOU decide if it is good or not hiding the super static method, not the creator of the superclass. That�s my argument. Am I clear now?
                            • 11. Re: static final method
                              JosAH
                              You are subclassing a class. YOU decide if it is good
                              or not hiding the super static method, not the
                              creator of the superclass. That�s my argument. Am I
                              clear now?
                              Nope, the creator of the superclass determines whether or not a non-
                              static method can be overridden by using the final keyword and so can
                              s/he decide whether or not a static method can be hidden by applying
                              that same keyword to the static method.

                              Jos
                              • 12. Re: static final method
                                807606
                                I asked that because I think "Gosling" defined that rule in JLS, that says "A method can be declared final to prevent subclasses from overriding
                                or HIDDING it." just to see if someone in the world invents a good reason for the usage of final keyword in this situation, like if you said "let me see the things happen...", but nobody invented that yet, for the sadness of "Gosling". And I doubt that there is a good reason for that.

                                The reply 2 (mine) is not clear, I admit. In this reply, I am citing "hide" in the wrong meaning, but I hope that now, with my other replies, my intention is clear.
                                • 13. Re: static final method
                                  807606
                                  You are subclassing a class. YOU decide if it is
                                  good
                                  or not hiding the super static method, not the
                                  creator of the superclass. That�s my argument. Am
                                  I
                                  clear now?
                                  Nope, the creator of the superclass determines
                                  whether or not a non-
                                  static method can be overridden by using the final
                                  keyword and so can
                                  s/he decide whether or not a static method can be
                                  hidden by applying
                                  that same keyword to the static method.

                                  Jos
                                  What would be the destruction caused by anyone who hides the super static method? There isn�t any destruction, nor any bad designing damage. Actually, the final keyword for static methods is the bad thing here. Everybody should avoid this keyword in this situation, for the designing goodness. Of course, considering that all the programmers are experienced, and know Java very well.
                                  • 14. Re: static final method
                                    796440
                                    You are subclassing a class. YOU decide if it is good
                                    or not hiding the super static method, not the
                                    creator of the superclass. That�s my argument. Am I
                                    clear now?
                                    The creator of a class decides what that class' members' visibility should be. This is the way it is and this is the way that makes the most sense.
                                    1 2 Previous Next