static
. This means the member belongs to the class rather than to an individual object. If a variable is static, then when any object in the class changes the value of the variable, that value changes for all objects in the class.
One possible use of static methods and members is to add version information to a class. A static String
field called version
can be used to store the version information and a static method called getVersion()
can return it. Since the version is a property of the class rather than of an object it is properly static.
Another good reason to make a method static is if it neither accesses nor modifies any of the instance (non-static) fields of a class and it does not invoke any non-static methods in the class. This is common in calculation methods like a square root method that merely operate on their arguments and return a value.