Creating a Java Object

 

Objects can be created only for those classes which have a public constructor. Class instances cannot be created for classes which have only private constructors, e.g. an object cannot be created for the Pattern class.

 

To be able to call object methods of a Java class, you need to instantiate this class instance. To create an instance of a Java class, call the following method for a previously imported class:

 

<ClassName>.create()

 

The value returned should be assigned to an object reference variable of the appropriate type. For example:

 

IMPORT JAVA java.lang.StringBuffer

...

DEFINE sb StringBuffer

LET sb = StringBuffer.create()

 

You cannot use a full-qualifier class name when invoking the create() method.

 

 

 

If the Java class constructor expects parameters, these should be passed to the create() method. In the example below the file name is passed as the parameter:

 

IMPORT JAVA java.io.File

...

DEFINE f File

LET f = File.create("filename")

 

You can create more than one object for the same class, if the class resources allow you to do so, e.g.:

 

IMPORT JAVA java.lang.StringBuffer

...

DEFINE sb1, sb2 StringBuffer

LET sb1 = StringBuffer.create()

LET sb2 = StringBuffer.create()

 

In this case these variables will reference two unique StringBuffer objects. The 4GL also allows the assignment of the value of one object reference variable to another, e.g.:

 

LET sb2 = sb1

 

In the above case the object referenced previously by variable sb2 will be implicitly deleted and both variables will refer to one and the same object which was previously referenced only by sb1.