Instantiating Java Objects

The JavaLoadObject function is used to instantiate Java objects, and additionally obtain handles on Java objects. Such handles can later be used to call methods on objects or they can be used as input parameters for other method calls. For the JavaLoadObject function, you must provide fully qualified names of classes to instantiate them as parameters. The ‘/’ character must be used as a package separator.

For example, to instantiate a Java object of class java.util.ArrayList in a TMain transaction, the BDL code looks like this:

  transaction TMain 
  var hList : number; 
  begin 
    // invoking constructor of java.util.ArrayList 
    hList := JavaLoadObject("java/util/ArrayList"); 
    // use the object 
    // ... 
    // free reference on the object when done 
    JavaFreeObject(hList); 
  end TMain;

If a Java class has a constructor that takes parameters, use JavaSet* functions to set those parameters. In the Java Framework, constructor calls are treated like static method calls, so use the constant JAVA_STATIC_METHOD instead of an object handle as the first parameter for JavaSet* functions.

For example, to invoke the constructor ArrayList(int initialCapazity) of class java.util.ArrayList in a TMain transaction, the BDL code looks like this:

  transaction TMain 
  var hList : number; 
  begin 
    // putting necessary parameters on the stack 
    JavaSetNumber(JAVA_STATIC_METHOD, 100); 
    // invoking constructor of java.util.ArrayList 
    hList := JavaLoadObject("java/util/ArrayList"); 
    // use the object 
    // ... 
    // free reference on the object 
    JavaFreeObject(hList); 
  end TMain;

The JavaLoadString function was introduced for convenience; it simplyfies the instantiation of java.lang.String objects.

To create a custom timer measure for a Java object instantiation, specify the timer name as the third optional parameter of the respective JavaLoadObject or JavaLoadString command.