FIELD Verb - BBj
The BBj Custom Object FIELD verb shares a name with the original Field Verb, but is otherwise unrelated.
BBj-Specific Information
Syntax
FIELD accesslevel {STATIC} typename fieldname {= expr}
Description
In BBj 6.0 and higher, the FIELD verb defines a Custom Object field.
Parameter |
Description |
---|---|
accesslevel |
The field access level must be specified, and must be PUBLIC, PRIVATE or PROTECTED. A PRIVATE field is only visible to code within the same class. A PROTECTED field can also be accessed from any class that EXTENDS this class. A PUBLIC field can be accessed from any code inside or outside this or any other class. Within the original defining class, fields can be accessed using the "#fieldname" notation. Besides that defining class, only code in child classes (those that extend the parent class) can use the "#fieldname" notation, and even then only if the parent's field has public or protected visibility; all access to a FIELD from outside of the parent or child classes must use the getter and setter methods, described below. |
STATIC |
If specified, this indicates that the field being defined is a static field. Static fields can be accessed through getter and setter methods on the class name; there's no need to instantiate an instance of the class. Within a BBj session, all references to a given static field are shared, and return the same value. |
typename |
The type name can be any of the following:
|
fieldname |
The field name must begin with a letter or underscore, can contain any combination of letters, numbers, and underscores, and must specify a suffix compatible with the specified type name, consistent with standard BBx variable rules:
|
expr |
Any valid BBj expression. Field initialization will generate a runtime error if the expression does not evaluate to a value that can be assigned to the type of the field being defined. |
Notes
All BBj Custom Object names (interface name, class name, method name, field name) are case-sensitive.
The FIELD verb must be the only statement on the line (with one exception: it can end with a ; REM comment).
The FIELD verb is a syntax error if it appears inside a method definition or outside a class definition.
Getter and Setter (Accessor) Methods
BBj
automatically generates an implicit getter method and an implicit setter
method for each field. If a field has the name
ID and the type BBjNumber,
then the getter method will have the name getID and will return a BBjNumber. The setter method will have the name setID and will accept a single parameter of type and will accept a single parameter of type .
The accesslevel of the getter method and the setter method will be the
same as the
declared accesslevel of the field. So if the field ID is declared to be private then the implicit
methods "BBjNumber
getID()" and "void setID(BBjNumber ID)" will only be accessible
within the methods of the class in which the ID field is defined. If the field ID is
declared to be public then the getter and setter methods will be accessible in any
code. If the field ID is protected then the getter and setter methods
will only be accessible in methods of the class and in methods of subclasses
(classes that EXTEND the defining class). Any explicitly defined
method with the same method signature overrides the implicitly generated
method.
For example, assume the following trivial class with a couple of fields:
class public Sample |
When we include the generated getter and
setter methods, that class acts like this internally (although the generated
getter and setter methods
are never shown):
class public Sample |
Because of the need to generate these getter
and setter methods for each defined field, it is illegal to define two
fields with the same base name
but different suffixes. For example, this is not legal:
class public Sample |
That illegal class would report a syntax error on what it considers to be a duplicate field name (X$), because its getX() conflicts with the generated getter method of the previously defined X.
Example 1
|
Example 2 (BBj 18.0 and higher)
|