Java Programming/Keywords/static

is a Java keyword. It can be applied to a field, a method or an inner class. A static field, method or class has a single instance for the whole class that defines it, even if there is no instance of this class in the program. For instance, a Java entry point has to be static. A static method cannot be. It must be placed before the variable type or the method return type. It is recommended to place it after the access modifier and before the keyword:

The static items can be called on an instantiated object or directly on the class:

Static methods cannot call nonstatic methods. The current object reference is also not available in static methods.

Interest

 * Static variables can be used as data sharing amongst objects of the same class. For example to implement a counter that stores the number of objects created at a given time can be defined as so:

The  variable is incremented each time an object is created.

Public static variable should not be used, as these become global variables that can be accessed from everywhere in the program. Global constants can be used, however. See below:


 * Static methods can be used for utility functions or for functions that do not belong to any particular object. For example:


 * See also Static methods