Java Programming/Keywords/static
From Wikibooks, the open-content textbooks collection
static is a keyword that states that all instances of a given class are to share the same variable/method. This is used for a constant variable or a method that is the same for every instance of a class, such as the methods in the Math class. The main method of a class is generally labelled static. The static keyword can also be used in the context of defining inner classes. These classes are not bound to an instance of the outer defining class.
No object needs to be created to use static variables or call static methods. Just put the class name before the static variable or method to use them.
Static methods cannot call non static methods. The this current object reference is also not available in static methods.
Syntact:
publicstaticvariableName; orstaticpublicvariableName; and for methods :publicstaticvoid methodName() { ... } orstaticpublicvoid methodName() { ... } To access them : ClassName.variableName = 10; ClassName.methodName();
For Example:
public static final double pi = 3.14159;
public static void main(String[] args)
{
...
}