13 Replies Latest reply: Dec 6, 2012 3:15 AM by gimbal2 RSS

    final keyword

    814889
      Most of the warnings in any code checking tools we get final keyword could be used for any String used in a method.
      Is this really required unless u have strong reason , sorry if this question sounds stupid but i am want to know
        • 1. Re: final keyword
          gimbal2
          Nope, its up to you. Final can help you to prevent programming mistakes, but you are in no way obligated to use it. Code checking tools tend to be quite picky (which is a good thing), but its still only a tool without a brain - you should always apply your own brain first before you start to do things and stuff.

          Side note: in some cases final can also help the compiler to make optimizations I believe, but I don't know the specifics so I may be spewing nonsense.
          • 2. Re: final keyword
            TPD-Opitz
            Vicky wrote:
            Most of the warnings in any code checking tools we get final keyword could be used for any String used in a method.
            Are you sure you correctly interpreted the message?
            I'd rather expect the checking tool to sugest externialisation of strings.
            Is this really required unless u have strong reason , sorry if this question sounds stupid but i am want to know
            My rule of thumb is: <ul>
            <li>I declare member properties final by any chance. (I like my objects beeing immutable).
            <li>Method parameters should also be declared final since reassigning them new values is somewhat "smelly", but I think this just clutters the method declaration.
            <li>I never declare local variables <b>final</b> (unless I use them in anonymous inner classes...).
            </ul>
            Maybe there's a technical reason to force final variables, but I don't know that.

            bye
            TPD
            • 3. Re: final keyword
              Kayaman
              This seems a bit overeager to me and would probably uglify the code further more than help.

              If you just have good programming practices, i.e. you don't reuse variables, then putting "final" everywhere is completely unnecessary.
              • 4. Re: final keyword
                baftos
                Maybe OP can give us the name of the tool and the exact message. I tried to Google for an answer and it's quite impossible, I only get results about why the String class is final.

                Edited by: baftos on Nov 22, 2012 10:18 AM
                • 5. Re: final keyword
                  aksarben
                  If using Eclipse, you can configure the IDE to automatically make eligible variables final (or not). Depends on whether you appreciate the extra help. Personally, I like it when my tools keep me from making dumb mistakes.
                  • 6. Re: final keyword
                    976999
                    Java compiler automatically joins any String constants that are concatenated using the "+" operator. It also calculates values of other types when final modifier is used for the constant and all of it's parts (and no other actions are required to know their values), ie.:

                    Declaration like this:
                      
                        final static int FOO = 1;
                        final static int BAR = 1;
                        final static int MOO = FOO + BAR;
                    Results in:
                        static final int FOO = 1;
                        static final int BAR = 1;
                        static final int MOO = 2;
                    However, declaration of Integers:
                        final static Integer FOO = 1;
                        final static Integer BAR = 1;
                        final static Integer MOO = FOO + BAR;
                    Will compile as (due to the fact, that first two constants are initialized with primitives and boxing is required):
                        static final Integer FOO;
                        static final Integer BAR;
                        static final Integer MOO;
                    
                       static 
                        {
                            FOO = Integer.valueOf(1);
                            BAR = Integer.valueOf(1);
                            MOO = Integer.valueOf(FOO.intValue() + BAR.intValue());
                        }
                    • 7. Re: final keyword
                      Kayaman
                      cezary_u wrote:
                      Java compiler automatically joins any String constants that are concatenated using the "+" operator.
                      What the he** does that have to do with anything?
                      • 8. Re: final keyword
                        976999
                        If you check what post was this answer to, you could see, that gimbal2 has put side note... and this is the 'anything' you've mentioned.
                        • 9. Re: final keyword
                          Kayaman
                          cezary_u wrote:
                          If you check what post was this answer to
                          No one checks those.
                          you could see, that gimbal2 has put side note... and this is the 'anything' you've mentioned.
                          Frankly, I wouldn't trust those optimizations in the first place. First of all, you're not citing any reliable source, secondly what really matters are the runtime optimizations handled by the JIT.
                          • 10. Re: final keyword
                            976999
                            Kayaman wrote:
                            cezary_u wrote:
                            If you check what post was this answer to
                            No one checks those.
                            Next time, i'll rememebr to put exact quote :)

                            Frankly, I wouldn't trust those optimizations in the first place. First of all, you're not citing any reliable source, secondly what really matters are the runtime optimizations handled by the JIT.
                            I've provided sample source code and result of compilation. That's true that compile time optimization probably would not influence the performance visibly, it's nice to know how it works :)

                            Cheers
                            • 11. Re: final keyword
                              jschellSomeoneStoleMyAlias
                              Kayaman wrote:
                              Frankly, I wouldn't trust those optimizations in the first place. First of all, you're not citing any reliable source
                              I believe the optimizations cited are required for a compliant compiler. That includes expressions of primitives, strings and final primitive variables. I suspect it has been that way for quite some time but I only looked at the most recent JLS.

                              From the JLS-7

                              4.12.4. final Variables
                              +"A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable. "+

                              15.28. Constant Expressions
                              +"A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly _and is composed_ using only the following:+
                              +...+
                              +-Literals of primitive type+
                              +...+
                              +-The additive operators+
                              +...+
                              +-Simple names (§6.5.6.1) that refer to constant variables (§4.12.4). "+
                              • 12. Re: final keyword
                                814889
                                Thank you
                                • 13. Re: final keyword
                                  gimbal2
                                  Kayaman wrote:
                                  cezary_u wrote:
                                  If you check what post was this answer to
                                  No one checks those.
                                  no one uses the search either, but they should. Some people should already know better.

                                  But indeed: proper quoting helps to avoid confusion.