random - Advantage of using static variables in Java -
say have 2 classes this:
class a{ private static random random = new random(); public a(){ // something. } public integer methodgetscalledquiteoften(){ return random.nextint(); } } class b{ private random random; public a(){ random = new random(); // something. } public integer methodgetscalledquiteoften(){ return random.nextint(); } }
in scenario both of them instantiated multiple times , both of these classes' instances' method methodgetscalledquiteoften
gets called lot, there real advantage/disadvantage (time, memory) in using static variable holds random()
in class opposed creating new random()
object in every single instance, in class b?
the application multithreaded , higher level of randomness think going static securerandom
. if real speed factor after profiling might choose else.
real advantage/disadvantages depend on real code. in other words, depends on how b
created compared how method called, etc. should profile application , see whether makes reasonable difference.
will more performant b? certainly. whether you'll ever notice depends on usage. use less memory b? certainly, whether care or not depends on how many instances of a/b you're keeping around.
really other consideration determinism. since don't specify seed random instance, take don't care whether can reproduce sequence of numbers random. it's worth noting...if have shared random, harder guarantee deterministic sequence of numbers instance of 1 per instance in b.
Comments
Post a Comment