The Hyper Programming Language

Class Fields

General

A class field is like a variable, except for that it is located inside a class instead of inside a procedure.

Semantics

A class field can be put anywhere you can put other members of a class like procedures, constructors and nested classes. This means that they can be public, protected or private. A field can also be static, which means that it is bound to the class itself, instead of to an instance of that class.

A field can have an initializer, which provides an initial value for the field. This initial value is determined at program startup for static fields, or at instantiation for other fields. A field can also be initialized by a constructor. In that case the initializer of the field will not be used. A field that has no initializer and is not initialized by a constructor, will be given the default value for its type. And that is of course determined by the default constructor for that type. (See default values for more info)

The fields in a class will be initialized in the order they are declared. This means that a field can use in its initializer only the fields that were already initialized. And keep in mind static fields are initialized before non-static fields.

Syntax

A field is declared on one line, like a variable. It starts with the var keyword, followed by the name of the field and then the type:

	var name : type

You can use an initializer:

	var name : type = initializer

You can create several fields that share their types and initial values. For two fields:

	var name1 & name2 : type = initializer

This can of course be generalized for more fields:

	var name1 & name2 & name3 & name4 & name5 : type = initializer

Examples

class X
  var a : int # the default value of a will be 0
  var b : int = 15 # the default value of b will be 15
  var c : bool = a < b # c will be true
  static var d : int # d will be initialized to 0
  static var e : int = a # ERROR : the value of a is not available because there is no class instance
  var f : int = g # ERROR: g is not yet initialized here
  var g : int = h # OK, h is initialized before g because it is static
  static var h : int = 44
end

See also


Valid HTML 4.01 Strict Valid CSS!