DECLARE {AUTO} type variable


In BBj 6.0 and higher, the DECLARE verb is used to indicate that a variable may only contain values of the specified type. The DECLARE verb introduces type-safety to the BBj language and allows detection of incompatible assignments at compile time when the type checker (bbjcpl -t) is used.




In BBj 8.0 and higher, the AUTO modifier signifies that assignments to the variable will automatically do a CAST() to the variable if the value (as opposed to the type) of the expression on the right hand side of an assignment is assignment compatible with the variable on the left hand side.


A BBj Object type, Custom Object type, or a Java class or primitive. Examples of BBj Objects include BBjString and BBjDataBoundGrid. Custom Objects are those created using the CLASS verb. Custom Objects must either be defined in the same program file, fully qualified, or locatable by a USE verb or config.bbx directive. Java types include classes such as java.lang.String and com.keypoint.PngEncoder. Java Objects must either be fully qualified or locatable by a USE verb or config.bbx directive.

In BBj 8.0 and higher, an @ suffix following a type name indicates a ClientObject reference.

In BBj 18.0 and higher, the DECLARE verb can declare a Java array object by appending one or more [] after a Java class or primitive type name.


A variable that will carry values as specified bytype. Valid variable names and suffixing based on type is explained in BBj Data Types.


If a variable is declared to be a specific type, then expressions that do not produce a type that is assignable to the declared type may not be assigned to the variable. Any attempt to assign an expression that is not of the correct type will result in a runtime error. In addition, the TypeChecker will generate an error when the code is type checked if the code includes a statement that assigns a value of incorrect type to the variable.

An exception to this rule is when the DECLARE statement includes an AUTO modifier. This allows assignments of expressions that produce a value with a type that is assignment compatible to the attached type of the variable.

A DECLARE statement that appears within a method body (see Method Verb) only affects the use of the declared variable within the method body. A DECLARE statement that occurs in 'open' code (not within a class) affects all usage of the variable within the program in which the DECLARE statement is found except for code that is within a method (i.e. the DECLARE statement only affects the region in which it is defined).

A DECLARE statement is similar to a DATA statement in that it does not need to be 'executed' in order to take effect. The presence of a DECLARE statement will attach a type to a variable just by being present in a program.

Example 1

myClass! = new MyClass()

class public MyClass
    field public static BBjColor MyStatic!
    field public BBjColor MyInstance!
    method public void myMethod(BBjColor p_declaredParameter!)
        declare BBjColor declaredLocal!
        declaredLocal! = p_declaredParameter!
        undeclaredLocal! = p_declaredParameter!

        rem ' Assigns to the fields
        #MyStatic! = p_declaredParameter!
        #MyInstance! = p_declaredParameter!

        rem ' Assigns to the fields
        #MyStatic! = declaredLocal!
        #MyInstance! = declaredLocal!

        rem ' Can't assign an undeclared value to a declared
        rem ' variable, field, or parameter. The following
        rem ' lines would all report type-check errors at runtime
        rem ' or at compile-time with type-checking (bbjcpl -t):
        rem '----------------------------------------
        rem 'declaredLocal! = undeclaredLocal!; rem ' type check error
        rem '#MyStatic! = undeclaredLocal!; rem ' type check error
        rem '#MyInstance! = undeclaredLocal!; rem ' type check error
        rem '----------------------------------------
        rem ' The #varName! syntax refers to a class field.

        rem ' These are undeclared local variables, so this is legal:
        MyStatic! = undeclaredLocal!
        MyInstance! = undeclaredLocal!


Example 2

use java.util.HashMap
declare MainClass MyMainClass!
declare auto Interface MyInterface!
MyMainClass! = new SubClass()

rem ' This would fail without the auto modifier.
MyInterface! = MyMainClass!
MyMainClass! = new MainClass()

rem ' This would fail because MainClass does not implement Interface:
rem ' MyInterface! = MyMainClass!

class public MainClass

    field private static HashMap hashMap! = new HashMap()
    method public static void addToMap(CustomObject obj!, BBjString A$)
        #hashMap!.put(obj!, A$)

    method public MainClass()
        #addToMap(#this!, "MainClass")


class public SubClass extends MainClass implements Interface
    method public void sayInterface()
        print "bingo"

interface public Interface
    method public void sayInterface()

Example 3 (BBj 8.0 and higher)

rem 'Print time and TimeZone of client machine

use java.util.Date
use java.text.DateFormat
declare Date@ clientDate!
declare DateFormat@ clientFormat!
clientDate! = new Date@()
clientFormat! = DateFormat@.getDateTimeInstance()
print clientFormat!.format(clientDate!)," ",
print clientFormat!.getTimeZone().getDisplayName()

Example 4 (BBj 18.0 and higher)

declare auto File file!
declare File[] files!
file! = new File(dir(""))
files! = file!.listFiles()
print files!.getClass().getTypeName(),files!.length
for i = 0 to files!.length-1
    file! = files![i]
next i