title: Advantage of using static variables in Java
body: <p>Say I have two classes like this:</p>
  
  <pre><code>class A{  
      private static Random random = new Random();  
  
      public A(){  
          // Do something.
      }
  
      public Integer methodGetsCalledQuiteOften(){
          return random.nextInt();
      }
  }
  
  class B{  
       private Random random;  
  
       public A(){  
           random = new Random();
           // Do something.
       }
  
       public Integer methodGetsCalledQuiteOften(){
           return random.nextInt();
       }
  }
  </code></pre>
  
  <p>In a scenario where both of them get instantiated multiple times and both of these classes' instances' method <code>methodGetsCalledQuiteOften</code> gets called a lot, is there any <em>real</em> advantage/disadvantage (time, memory) in using a static variable that holds <code>Random()</code> in class A as opposed to creating a new <code>Random()</code> object in every single instance, like in class B?</p>
  
  <p>The application is multithreaded and higher level of randomness is so I think I am going with static <code>SecureRandom</code>. If that will be a real speed factor after profiling I might choose something else.</p>