Static modifier

The static modifier can be applied to fields and methods. It denotes that the method or the field is bound to the class, rather than the object.

Static fields

Static fields store some data globally and the data is shared across all objects of the type, if there are any. The value can be accessed using the type name or with any object of the type, just like usual fields:

type zomg
{
  static int @data
}

// setting by name
zomg:data = 1

// getting by object
var item = new zomg
print item.data 

Static methods

Static methods are about the same as usual methods, the only difference being that they cannot access fields and methods of the type that are not marked as static. Just like a static field, they can be accessed either by class name or by object.

type zomg
{
  static int @data

  static void inc
  {
    @data += 1
  }
} 


Another awesome thing about static methods is that they can be made globally visible.

Last edited Mar 15, 2011 at 2:45 PM by impworks, version 4

Comments

No comments yet.