If you need to use a method in a Java class only once, you can use the javaLib.invoke system function.
This example calls the method random. This method belongs to the class java.lang.Math and returns a random number between 0 and 1. In this case, the class resembles an EGL library and the method works like a function in this library.
The only methods you can call in this way are the methods defined as static. "Static" is a Java term that has no parallel in EGL; in this context, it means that the method is ready to be called at any time and needs no initialization before use.
myNumber float;
myNumber = javaLib.invoke("java.lang.Math", "random");
sysLib.writeStderr(strLib.formatNumber(myNumber));
To pass a parameter to the method, use the argument parameter of javaLib.invoke().
If you plan to use methods in a Java class several times, or if the method that you want to call is not defined as static, you should create an ExternalType part, as in "Creating an ExternalType part to represent a class" that follows. However, for compatibility with migrated programs, and with a little less EGL code, you can use other functions in javaLib to initialize and use a class multiple times, even its non-static methods.
//Error! The method nextInt() is not static. myInt int = javaLib.invoke("java.util.Random", "nextInt");
javaLib.storeNew("myRandomGenerator" AS "objId:java", "java.util.Random");
myInt int = javaLib.invoke("myRandomGenerator" AS "objId:java", "nextInt");
In these examples, the string myRandomGenerator is an identifier that represents the class. Each time you use the class, you tell EGL that this identifier refers not to an EGL part or a string literal but a Java class with the code AS "objId:java". For more information, see as operator. For more details on calling Java classes and methods, see Java access functions.
If you plan to use the class often, or if the method that you want to use is not defined as static, you can define an EGL ExternalType part, an EGL part that represents a Java class directly. After you have defined the ExternalType part, you can create variables based on it and use those variables just like you would use the names of EGL libraries.
This example uses the class java.util.Random. This class has a non-static method named nextInt which returns a random integer. Again, for the purposes of EGL programming, imagine that the class is a library and the methods are functions.
ExternalType Random type JavaObject {packageName = "java.util", javaName = "Random"} //prototypes go here endNote that the name of the Java class is divided into two properties:
ExternalType Random type JavaObject {packageName = "java.util", javaName = "Random"} function nextInt() returns(int); endAgain, function prototypes in Java are similar to function prototypes in EGL, such as those in an Interface part. They list the function or method name, its arguments, and its return value, but have no internal logic. Here, a prototype links a function in the ExternalType part to a method in the Java class.
Also, you must be careful to match Java types to compatible EGL types and vice versa. Tables associating EGL parts and Java parts are available in EGL primitives and Java.
ExternalType Random type JavaObject {packageName = "java.util", javaName = "Random"} function nextInt() returns(int); constructor(); end
myRandomGenerator Random = new Random();The code new Random() is actually a function call to the constructor() function prototype in the part definition, which in turn refers to the constructor of the Java class.
myInt int = myRandomGenerator.nextInt(); sysLib.writeStderr(strLib.formatNumber(myInt));
/* Writes a random integer to the console using an ExternalType part */ program ExternalTypeTest type BasicProgram function main() myRandomGenerator Random = new Random(); myInt int = myRandomGenerator.nextInt(); SysLib.writeStderr(StrLib.formatNumber(myInt)); end end externalType Random type JavaObject {packageName = "java.util", javaName = "Random"} constructor(); function nextInt() returns(int); end
For more details on the ExternalType part, see ExternalType part.