![]() The following example shows how you can access the class: The Java.type() function takes a string with the fully qualified Java class name, and returns the corresponding JavaClass function object. To avoid the disadvantages of the approach previously described, Nashorn defines the Java global object that has several functions for working with Java classes. The following example shows how this conditional check works: ![]() To avoid this, use the typeof operator to conditionally test that the construct you are trying to access is interpreted as a function object. You might not be aware of this until an error is thrown when you attempt to use it as a class. If you misspell a class name, Nashorn assumes that you provided a package name, and interprets it as a JavaPackage object instead of a JavaClass function object. There is no special syntax for creating Java arrays. The traditional approach for accessing Java packages and classes is intuitive and straightforward, but at the same time, it can be inefficient, limited, and error-prone for the following reasons:Įach property access has a cost, so accessing a package or class in a deep hierarchy can be slow. For more information about instantiating a class, see Creating Java Objects. The following example shows how you can access the java.lang package and the class:Īs you can see from the previous example, Nashorn interprets Java packages as JavaPackage objects, and Java classes as JavaClass function objects, which can be used as constructors for the classes. They have aliases that correspond to properties of the Packages object. For your convenience, there are global objects defined for each of the standard Java packages: com, edu, java, javafx, javax, and org. The following example shows how you can access the MyPackage package and its M圜lass class if MyPackage.jar is in your class path:Īccessing standard Java packages and classes is more straightforward than accessing custom packages and classes. The predefined top-level Packages object enables you to access Java packages and classes using their fully qualified names, as if they are properties of the Packages object. There are two approaches to access packages and classes using Nashorn: the traditional approach is to use the Packages global object, and the recommended approach is to use the Java global object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |