8 Replies Latest reply: Oct 15, 2012 12:03 AM by DrClap RSS

    Random vs final Random

    967888
      Why is a random object typically declared final?
      What's the difference between:

      a. final Random randomNumbers = new Random();
      b. Random randomNumbers = new Random();

      Edited by: 964885 on Oct 11, 2012 11:21 PM
        • 1. Re: Random vs final Random
          sabre150
          I wouldn't say it is typically declared 'final'. It is my experience that people rarely use 'final' and I am in a minority in that I declare all variables I possibly can to be final. I do it so that the reader of the code knows that if declared 'final' the value will not change and if not declared 'final' it may change. This approach has been said to clutter the code with superfluous 'final' tags and to some extent I agree but to my mind the advantages outweigh this disadvantage.


          Note - the 'final' on the Random declaration will make no difference to the numbers generated or the speed in which they are generated. 'final' is really just a hint to the compiler that may allow some optimisations (though I bet not significant optimisations).
          • 2. Re: Random vs final Random
            EJP
            In this particular case it's quite a good idea, in that it shows that the same Random will be used to generate all random numbers in this scope, which gives a better guarantee of randomness.
            • 3. Re: Random vs final Random
              967888
              After doing some thinking I came to the conclusion that final does not affect the outcome of the random number generation process. All that it does is to prevent any changes to the Random object properties and methods.

              For instance, by using keyword final the seed would never change, even if the setSeed() method would attempt to do so. Any subclass attempt to override the method would be useless.

              My conclusion is that it's a good idea to use final.
              • 4. Re: Random vs final Random
                gimbal2
                manohoo wrote:
                For instance, by using keyword final the seed would never change, even if the setSeed() method would attempt to do so.
                Prove it.

                final does NOT make objects immutable. It simply makes it so that the object reference cannot be reassigned to another Random object.
                • 5. Re: Random vs final Random
                  967888
                  You are right.
                  So... what is the answer to my original question? What are the pros/cons of using or not using keyword final when creating a Random object?

                  Edited by: manohoo on Oct 13, 2012 11:03 AM
                  • 6. Re: Random vs final Random
                    TPD-Opitz
                    manohoo wrote:
                    So... what is the answer to my original question? What are the pros/cons of using or not using keyword final when creating a Random object?
                    As the others wrote: there is no difference as fahr as the functionality of the refered object (a Random in your case) is concerned.

                    bye
                    TPD
                    • 7. Re: Random vs final Random
                      sabre150
                      manohoo wrote:
                      You are right.
                      So... what is the answer to my original question? What are the pros/cons of using or not using keyword final when creating a Random object?
                      I thought that I had covered much of that in the first response and others have elaborated on my response !!!! I can't see what else can be said.
                      • 8. Re: Random vs final Random
                        DrClap
                        sabre150 wrote:
                        I thought that I had covered much of that in the first response and others have elaborated on my response !!!! I can't see what else can be said.
                        What else can be said is this, since it wasn't explicitly said: Variables Are Not Objects, and Objects Are Not Variables.

                        In particular, declaring a variable final has no effect on an object to which that variable happens to contain a reference. (Remember, variables are not objects. Instead, they contain references to objects.)