Java interface allows you to call Java methods from within a 4GL program. To able to call class methods you need to import the corresponding Java class and declare a variable referencing this class. To be able to call object methods, you need also create a Java object for the imported class.
Java interface also allows you to access the data stored in Java class and object fields and to pass them to 4GL variables.
Class methods can be called without instantiating an object of the class. This way of calling a method is mainly used for the methods declared with the static keyword.
To call a class method the class should be previously imported using the IMPORT JAVA statement. You should also declare an object reference variable as described above. But you need not instantiate an object of this class.
To call a class method, the class name must be used as the prefix. If we use the class methods, we assign the result directly to the corresponding object reference variable.
<ClassName>.method()
For example:
IMPORT JAVA java.io.File
...
DEFINE f File
LET f = File.createTempFile("prefix", "suffix")
Object methods can only be called once a class has been instantiated as an object and the object reference has been assigned to a variable. These methods are usually used for the methods without the static keyword.
To call an object method, the variable name must be used as the prefix.
<InstantiatedVariable>.method()
For example:
IMPORT JAVA java.io.File
...
DEFINE f File
DEFINE s STRING
LET f = File.create("filename") -- we instantiate the variable first
LET s = f.mkdir()
The result returned by such a method should be assigned to another 4GL variable.
The value of a class field can be read without instantiating an object of the class. Like with the class methods, you need to import the class and declare the corresponding variable.
To access a class field, the class name must be used as the prefix.
<ClassName>.ClassField()
For example:
IMPORT JAVA java.io.File
...
DEFINE s STRING
LET s = File.pathSeparator
Object fields can only be read once a class instance has been instantiated, and the object reference has been assigned to a variable.
To access an object field, this variable name must be used as the prefix. The value returned must be assigned to a 4GL variable.
<InstantiatedVariable>.ObjectField()
For example:
IMPORT JAVA java.awt.Point
...
DEFINE p Point
DEFINE x_coord,y_coord INTEGER
# instantiate the object reference variable
LET p = Point.create(1,2)
LET x_coord = p.x -- access the field, returns 1
LET y_coord = p.y -- access the field, returns 2
In the example above p is the variable which contains the class object instantiated using the create(1,2) method - a point initialized at the given coordinates - 1,2. Also x in p.x and y in p.y are the names of the public fields of the Point class.