Core JavaScript Reference 1.5: | 9 | WebReference

Core JavaScript Reference 1.5: | 9

Previous     Contents     Index     Next     
Core JavaScript Reference 1.5





JavaObject

The type of a wrapped Java object accessed from within JavaScript code.


Core object  

 

Implemented in  

JavaScript 1.1, NES 2.0  


Created by
Any Java method which returns an object type. In addition, you can explicitly construct a JavaObject using the object's Java constructor with the Packages keyword:

new Packages.JavaClass(parameterList)

where JavaClass is the fully-specified name of the object's Java class.


Parameters


parameterList

 

An optional list of parameters, specified by the constructor in the Java class.  


Description
The JavaObject object is an instance of a Java class that is created in or passed to JavaScript. JavaObject is a wrapper for the instance; all references to the class instance are made through the JavaObject.

Any Java data brought into JavaScript is converted to JavaScript data types. When the JavaObject is passed back to Java, it is unwrapped and can be used by Java code. See the Core JavaScript Guide for more information about data type conversions.


Property Summary
Inherits public data members from the Java class of which it is an instance as properties. It also inherits public data members from any superclass as properties.


Method Summary
Inherits public methods from the Java class of which it is an instance. The JavaObject also inherits methods from java.lang.Object and any other superclass.


Examples
Example 1. Instantiating a Java object in JavaScript.

The following code creates the JavaObject theString, which is an instance of the class java.lang.String:

var theString = new Packages.java.lang.String("Hello, world")

Because the String class is in the java package, you can also use the java synonym and omit the Packages keyword when you instantiate the class:

var theString = new java.lang.String("Hello, world")

Example 2. Accessing methods of a Java object.

Because the JavaObject theString is an instance of java.lang.String, it inherits all the public methods of java.lang.String. The following example uses the startsWith method to check whether theString begins with "Hello".

var theString = new java.lang.String("Hello, world")
theString.startsWith("Hello") // returns true

Example 3. Accessing inherited methods.

Because getClass is a method of Object, and java.lang.String extends Object, the String class inherits the getClass method. Consequently, getClass is also a method of the JavaObject which instantiates String in JavaScript.

var theString = new java.lang.String("Hello, world")
theString.getClass() // returns java.lang.String


See also
JavaArray, JavaClass, JavaPackage, Packages


Previous     Contents     Index     Next     
Content is available under these licenses.
Last Updated September 28, 2000