Java Reference
In-Depth Information
Chapter 6
Using Java in Scripting
Languages
In this chapter, you will learn:
•
How to import Java classes into scripts
•
How to create Java objects and use them in scripts
•
How to call overloaded methods of Java objects
•
How to create Java arrays
•
How to extend a Java class and implement Java interfaces
in scripts
•
How to invoke superclass methods of an object from scripts
Scripting languages allow using Java class libraries in scripts. Each scripting language
has its own syntax for using Java classes. It is not possible, and is outside the scope of this
book, to discuss the syntax of all scripting languages. In this chapter, I will discuss the
syntax of using Java constructs in Nashorn.
Importing Java Types
There are four ways to import Java types into Nashorn script:
•
Using the
Packages
global object
•
Using the
type()
method of the
Java
global object
•
Using the
importPackage()
and
importClass()
functions
JavaImporter
in a
with
clause
Of the four types of importing Java types, the second type, using the
type()
method
of the global Java object, is preferred. The following sections will describe the four ways of
importing Java type in script in detail.
•
Using a
Search WWH ::
Custom Search