Section 24.96. JavaClass: JavaScript representation of a Java class


24.96. JavaClass: JavaScript representation of a Java class

LiveConnect

24.96.1. Synopsis

 javaclass.static_member   // Read or write a static Java field or method new javaclass(...)        // Create a new Java object 

24.96.2. Properties

Each JavaClass object contains properties that have the same names as the public static fields and methods of the Java class it represents. These properties allow you to read and write the static fields of the class and invoke the static methods of the class. Each JavaClass object has different properties; you can use a for/in loop to enumerate them for any given JavaClass object.

24.96.3. Description

The JavaClass object is a JavaScript representation of a Java class. The properties of a JavaClass object represent the public static fields and methods (sometimes called class fields and class methods) of the represented class. Note that the JavaClass object does not have properties representing the instance fields of a Java class; individual instances of a Java class are represented by the JavaObject object.

The JavaClass object implements the LiveConnect functionality that allows JavaScript programs to read and write the static variables of Java classes using normal JavaScript syntax. It also provides the functionality that allows JavaScript to invoke the static methods of a Java class.

In addition to allowing JavaScript to read and write Java variable and method values, the JavaClass object allows JavaScript programs to create Java objects (represented by a JavaObject object) by using the new keyword and invoking the constructor method of a JavaClass.

The data conversion required for communication between JavaScript and Java through the JavaClass object is handled automatically by LiveConnect. See Chapter 12 for full details.

24.96.4. Usage

Bear in mind that Java is a typed language. This means that each of the fields of an object has a specific datatype that is set to values of only that type. Attempting to set a field to a value that is not of the correct type results in a JavaScript error or exception. Attempting to invoke a method with arguments of the wrong type also causes an error or exception.

24.96.5. Example

java.lang.System is a JavaClass object that represents the java.lang.System class in Java. You can read a static field of this class with code like the following:

 var java_console = java.lang.System.out; 

You can invoke a static method of this class with a line like this one:

 var version = java.lang.System.getProperty("java.version"); 

Finally, the JavaClass object also allows you to create new Java objects:

 var java_date = new java.lang.Date( ); 

24.96.6. See Also

getClass( ), JavaArray, JavaObject, JavaPackage; Chapter 12




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net