In the context of class attributes, has a different meaning. If you have a field like:static
private static int sharedAttribute;
then, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.
Thus said, you might understand that this is bad in many cases, because it can easiy turn into an undesired side-effect: changing object also affects and you might end up wondering why changed with no apparent reasons. Anyway, there are cases where this behaviour is absolutely desirable:a
b
b
- class constants: since they are , having all the classes access the same value will do no harm, because no one can change that. They can save memory too, if you have a lot of instances of that class. Not sure about concurrent access, though.
const
- variables that are intended to be shared, such as reference counters &co.
static
vars are instantiated before your program starts, so if you have too many of them, you could slow down startup.
A method can only access attributes, but think twice before trying this.static
static
Rule of thumb: don't use , unless it is necessary and you know what you are doing or you are declaring a class constant. static