JavaObject

Summary

Core Object

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

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)

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

Parameters

parameterList 
An optional list of parameters, specified by the constructor of 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 1.5 Guide for more information about data type conversions.

Properties

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.

Methods

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: 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: 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: 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

Document Tags and Contributors

 Last updated by: fscholz,