METHOD Verb

Syntax

METHOD accesslevel {STATIC} {typename} methodname(parameterlist)

Description

In BBj 6.0 and higher, the METHOD verb marks the beginning of a Custom Object method definition, which must end with a METHODEND verb.

Parameter

Description

accesslevel

The method access level must be specified, and must be PUBLIC, PRIVATE or PROTECTED. A PRIVATE method is only visible within the class definition. A PROTECTED method is visible to code within the class definition, and in methods of classes that extend the class. A PUBLIC method can be accessed from all code, including code in other files.

STATIC

If specified, this indicates that the method being defined is a static method. A static method can only access fields that are declared static. Any attempt to access a non-static field will generate a runtime error.

Static methods can be accessed directly against the class name; there's no need to instantiate an instance of the class.

typename

The type name specifies the data type returned by the method. If the method name corresponds to the class name, this method is a constructor, and the type name must not be specified. For all other method names, the type name must be specified, and 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 specified in a USE statement in the same program file.

  • A fully qualified Java type likejava.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).

  • The keyword void to indicate that the method does not return a value

methodname

The method name must begin with a letter or underscore and can contain any combination of letters, numbers and underscores.

parameterlist

A comma-delimited list of zero or more parameters. Each parameter consists of a parameter type, followed by white space, followed by a parameter name.

The parameter type 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 specified in a USE statement in the same program file.

  • A fully qualified Java type likejava.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).

The parameter 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 parameter type, consistent with standard BBx variable rules:

  • If the parameter type is BBjNumber, the parameter name must have no suffix or the suffix "!".

  • If the parameter type is BBjString, the parameter name must have the suffix "$" or "!".

  • If the parameter type is BBjInt, the parameter name must have the suffix "%" or "!".

  • If the parameter type is anything other than BBjNumber, BBjString, or BBjInt, the parameter name must have the suffix "!".

Notes

All BBj Custom Object names (interface name, class name, method name, field name) are case-sensitive.

The METHOD verb must be the only statement on the line (with one exception: it can end with a ; REM comment).

The METHOD verb is a syntax error unless the program also contains a METHODEND verb on a subsequent line.

The METHOD verb may not be nested within another METHOD.

A method definition (a block of code between METHOD and METHODEND) defines a variable scope. The only variables that are visible within that scope are the parameters specified in the method parameter list and the fields defined in the class. Labels and local variables in a method are only visible within that method.

The method name together with its parameter list is known as the “method signature”. Methods with the same name, but different parameter lists are considered unique and may coexist. The return type is not part of the method signature.

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