Let's say I have two classes:
class A{
private static Random random = new Random();
public A(){
}
public Integer methodGetsCalledQuiteOften(){
return random.nextInt();
}
}
class B{
private Random random;
public A(){
random = new Random();
}
public Integer methodGetsCalledQuiteOften(){
return random.nextInt();
}
}
In a scenario in which both of them get an instance several times, and the method method of the instances of these classes methodGetsCalledQuiteOftengets a lot of calls, is there any real advantage / disadvantage (time, memory) when using a static variable that runs Random()in class A as opposed to creating a new object Random()in each individual instance, for example, in class B?
The application is multi-threaded and has a higher level of randomness, so I think I'm going with static SecureRandom. If this is a real speed factor after profiling, I could choose something else.