When to use static variables/methods and when to use instance variables/methods in Java?

user3133542 picture user3133542 · Jan 16, 2014 · Viewed 33.1k times · Source

i would like to ask the question when it would be advantageous to use static variables/methods or in the other case instance variables/methods in Java?

I know that it depends on the certain case (like programming util-classes as static methods), but can we declare something like a general strategy?

Answer

TheLostMind picture TheLostMind · Jan 16, 2014

At novice level :

Use instance variables when : Every variable has a different value for different object. E.g. name of student, roll number etc..

use static variables when : The value of the variable is independent of the objects (not unique for each object). E.g. number of students.