Method syntax
>>-+-----------+-static-+-------+-+--------------+-+--------+-> +-public----+ '-final-' '-synchronized-' '-native-' +-protected-+ '-private---' >-ResultType-Identifier-(-+---------------------+-)-> '-FormalParameterList-' >-+----------------------+-MethodBody->< '-throws-ClassTypeList-'
Field syntax
>>-+-----------+-+----------+-static-+-----------+-Type-> +-public----+ +-final----+ '-transient-' +-protected-+ '-volatile-' '-private---' >-+-Identifier--------------+-=-+-Expression-------+-;->< | v-----' | '-ArrayInitializer-' '-ArrayIdentifier-+-[-]-+-'
Description
The static modifier is used to create class methods and variables. If a field
is declared static, there exists exactly one representation of the field, no matter how
many instances of the class may eventually be created. A static field, sometimes called a
class variable, is created when the class is initialized.
A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. Class methods are available to any instance of the class, as well as to the class itself. They can also be made available to other classes. An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compilation error. If a static method is declared abstract, a compilation error occurs.
Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers for class variables, may be used to initialize the class variables of the class. A static initializer cannot complete abruptly with a checked exception; a compilation error occurs. A class can have any number of static initialization blocks that appear anywhere in the class body. The runtime system guarantees that static initialization blocks and static initializers are called in the order (left-to-right, top-to-bottom) that they appear in the source code.
The static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope. This restriction is designed to catch, at compile time, circular or otherwise malformed initializations.
If a return statement appears anywhere within a static initializer, a compilation error occurs. If the keyword this or the keyword super appears anywhere within a static initializer, a compilation error occurs.
Examples
A static field is declared as follows:
static int Sample;
A static method is declared as follows:
private static void Samplemethod () { ... }
The following is an example of a static initialization block:
import java.util.ResourceBundle; class Errors { static ResourceBundle errorString; static { try { errorString = Resourcebundle.getBundle ("ErrorStrings"); } catch (java.util.MissingResourceException e) { // error recovery code here } } }
The errorStrings resource bundle must be initialized in a static initialization block. This is because error recovery must be performed if the bundle cannot be found. Also, errorStrings is a class member and it does not make sense for it to be initialized in a constructor. As the previous example shows, a static initialization block begins with the static keyword and is a normal block of Java code enclosed in braces {}.
Syntax diagrams
abstract keyword
class keyword
final keyword
native keyword
private keyword
protected keyword
public keyword
super keyword
synchronized keyword
this keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.