I read that invoking instance methods on static variables/methods is considered bad practice( I don't know exactly why but that's not the question anyway).
Eg:
class Dice
{
private int value;
private static Random randomizer=new Random();
public int setAndget_DiceValue()
{
value=randomizer.nextInt(6)+1; //Accessing static variable via instance class. Any alternatives?
return value;
}
}
What are then alternatives to modifying a static variable that doesn't involve invoking instance method?
Even if I used a static method on randomizer
, I would then have to use that instance method on a static method which is still considered bad practice.
Edit: Some places I first saw about this being considered a bad practice:
1 ) Writing to a static variable in an instance method, why is this a bad practice?
2 ) A comment to the OP's post: Can non-static methods modify static variables. He was a high-rep user so I was inclined to believe him.