static Modifier

Declares that a class member belongs to a class rather than to instances of the class.

static statement

Arguments

  • statement
    Required. A class member definition.

Remarks

The static modifier signifies that a member belongs to the class itself rather than to instances of the class. Only one copy of a static member exists in a given application even if many instances of the class are created. You can only access static members with a reference to the class rather than a reference to an instance. However, within a class member declaration, static members can be accessed with the this object.

Members of classes can be marked with the static modifier. Classes, interfaces, and members of interfaces cannot take the static modifier.

You may not combine the static modifier with any of the inheritance modifiers (abstract and final) or version-safe modifiers (hide and override).

Do not confuse the static modifier with the static statement. The static modifier denotes a member that belongs to the class itself rather than any instance of the class.

Example

The following example illustrates a use of the static modifier.

class CTest {
   var nonstaticX : int;      // A non-static field belonging to a class instance.
   static var staticX : int;  // A static field belonging to the class.
}

// Initialize staticX. An instance of test is not needed.
CTest.staticX = 42;

// Create an instance of test class.
var a : CTest = new CTest;
a.nonstaticX = 5;
// The static field is not directly accessible from the class instance.

print(a.nonstaticX);
print(CTest.staticX);

The output of this program is:

5
42

Requirements

Version .NET

See Also

Reference

expando Modifier

var Statement

function Statement

class Statement

static Statement

Concepts

Scope of Variables and Constants

Type Annotation

Other Resources

Modifiers