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:

  • A simple BBj Custom Object class name defined in the same program file.

  • A simple BBj Custom Object class name specified in a USE statement in the same program file.

  • A fully qualified BBj Custom Object class name in the form ::filename::classname.

  • The name of a Java type that can be located via a USE statement in the same program file.

  • A fully qualified Java type like java.util.HashMap.

  • A simple Java type that can be found in the java.lang package like Boolean.

  • A Java primitive type (byte, short, int, long, float, double, boolean, char).

  • In BBj 18 and higher, any of the Java types described above, followed by [] to indicate a Java array type, as shown in Example 2, below.

  • The name of an internal BBj type (BBjString, BBjNumber, BBjInt, or any defined BBjObject type like BBjButton).

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:

  • If the fieldtype is BBjNumber, the fieldname must have no suffix or the suffix "!".

  • If the fieldtype is BBjString, the fieldname must have the suffix "$" or "!".

  • If the fieldtype is BBjInt, the fieldname must have the suffix "%" or "!".

  • If the fieldtype is anything other than BBjNumber, BBjString, or BBjInt, the fieldname must have the suffix "!".

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
  field public BBjNumber ID
  field public BBjString Name$
classend

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
  field public BBjNumber ID
  method public void setID(BBjNumber ID)
    #ID = ID
  methodend
  method public BBjNumber getID()
    methodret #ID
  methodend
  field public BBjString Name$
  method public void setName(BBjString Name$)
    #Name$ = Name$
  methodend
  method public BBjString getName()
    methodret #Name$
  methodend
classend

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
  field public BBjNumber X
  field public BBjString X$
classend

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

rem ' Payroll

e1! = new Salaried()
e1!.setID(1)
e1!.setName("Mary Jones")
e1!.setMonthlySalary(5000)
e1!.print()
print "GL Account:",e1!.account()

e2! = new Hourly()
e2!.setID(2)
e2!.setName("John Smith")
e2!.setHourlyRate(15)
e2!.setHoursWorked(168)
e2!.print()
print "GL Account:",e2!.account()

interface public Address
  method public void address()
interfaceend

interface public Pay
  method public void print()
interfaceend

interface public GL
  method public BBjNumber account()
interfaceend

interface public Payable extends Pay, GL
  method public BBjNumber pay()
interfaceend

class public Employee
  field public BBjNumber ID
  field public BBjString Name$
classend

class public Salaried extends Employee implements Payable
  field public BBjNumber MonthlySalary
  method public BBjNumber pay()
    methodret #MonthlySalary
  methodend
  method public void print()
    print "Employee",#getID(),": ",#getName()
    print #pay():"($###,###.00)"
  methodend
  method public BBjNumber account()
    methodret 11111
  methodend
classend

class public Hourly extends Employee implements Payable
  field public BBjNumber HourlyRate
  field public BBjNumber HoursWorked
  method public BBjNumber pay()
    methodret #HourlyRate*#HoursWorked
  methodend
  method public void print()
    print "Employee",#getID(),": ",#getName()
    print #pay():"($###,###.00)"
  methodend
  method public BBjNumber account()
   methodret 22222
  methodend
classend

Example 2 (BBj 18.0 and higher)

declare Sample Sample!
Sample! = new Sample()
declare int[] ints!
ints! = Sample!.getInts()
ints! = Sample!.ints(10)
ints! = Sample!.info(new int[4])
print ints!.getClass().getTypeName(),ints!.length
declare Integer[] Integers!
Integers! = Sample!.Integers(8)
print Integers!.getClass().getTypeName(),Integers!.length
stop
class public Sample
  field public int[] Ints! = new int[4]
  method public int[] info(int[] ints!)
   print ints!.getClass().getTypeName(),ints!.length
   for i = 0 to ints!.length-1
     print "ints![",str(i),"]=",ints![i]
   next i
   methodret ints!
  methodend
  method public int[] ints(BBjNumber length)
    methodret new int[length]
  methodend
  method public Integer[] Integers(BBjNumber length)
    methodret new Integer[length]
  methodend
classend

See Also

Verbs - Alphabetical Listing