Static vs non-static class members

You need to think about static variables as belonging to the class, not to instances of the class.

If, in all instances of the class this variable should be identical, use a static variable.

If not, use an instance variable.

In general having public static variables is bad practice – it is a shared global resource and if you change it you need to synchronize access to it. Having global state is something you want to avoid as much as possible.

Leave a Comment