While I was working on a class which required random number generation, I was thinking about a way to create just one Random object for multiple uses.
I read about static factories in effective java and came up with this, but the more I think about, the less logic I see in this. Is this the best way? And does this make sure only one Random object gets created?
public static Random newInstance() {
return new Random();
}
public static void generateRandom() {
Random rand = newInstance();
//...
}
Another way is initialising it in the constructor, although I'm not sure if this is the best practice?
public static Random rand = new Random();
Aucun commentaire:
Enregistrer un commentaire