5 Replies Latest reply on May 9, 2003 10:04 AM by juhalindfors

    Why are you using final for argument's declaration

    naxius

       

      "naxius" wrote:
      Hi,

      Juste to be aware of this optimization, can you tell me / us
      why in the source code, we have very often the final
      keyword for the declaration of the argument's methods like :

      public StateMachine(final Model model) {
      ...
      }


        • 1. Re: Why are you using final for argument's declaration

           

          "Juha Lindfors" wrote:
          The argument name very often shadows a field name in the same class. Declaring the argument as final prevents you from accidentally assigning a value to the local arg reference when you wanted to assign to instance field reference.

          It is not really an optimization, it is used as a defensive programming technique. Personally I don't like it and don't use it. Others obviously feel differently :-)



          • 2. Re: Why are you using final for argument's declaration
            naxius

             

            "naxius" wrote:
            If declaring arguments as final is to prevent an accidentally assignment, there is another technique to do that :

            public void setXxx(int xxx) {
            this.xxx = xxx;
            }

            And that's it.

            I thought that the compiler was doing some optimization stuff ?


            Thank you.
            Alexandre


            • 3. Re: Why are you using final for argument's declaration

               

              "Juha Lindfors" wrote:
              Yes the point is that it is easy to forget the 'this' reference.


              • 4. Re: Why are you using final for argument's declaration
                hezekiel

                 

                "Hezekiel" wrote:
                Not if you use a decent editor (aka IntelliJ). It will highlight the questionable assignment (prints it with light grey) so it definately strikes out of the context.

                It also highlights all the unused variables and import statements similarly. It's funny how much trash you see in the sources of others when you open those with IntelliJ...

                Even the sources for java itself are cluttered with unused imports and variables ;-)

                Before commit to cvs:

                1. Hit CTRL+ALT+L (apply coding style)
                2. Hit CTRL+ALT+O (optimize imports)
                3. see that the source file is marked with green (no stupid assigments / no unused variables / valid javadoc)
                4. Right click on editor -> choose commit
                5. Write revision notes -> click ok.

                I can't stand people who use notepad to write java code!! =)


                • 5. Re: Why are you using final for argument's declaration

                   

                  "Juha Lindfors" wrote:
                  > Not if you use a decent editor (aka IntelliJ).

                  It is not easy to forget even if you use notepad (I do) but some people are not so easily convinced.