JavaObject.getClass() (Method/Java)

A JavaScript method for obtaining the class of a JavaObject.

Availability:

JavaScript - 1.2
Netscape - 4.0
JavaScript syntax:NmyJavaObject.getClass()

This returns the Java class of the encapsulated Java object. This is not necessarily the same as a JavaScript class name. In fact what is returned is an object reference to a class object which may need further work to extract the name of the class.

The example shows how a Java object is instantiated and then enquired to as of its class. Using the LiveConnect, the getClass() method requests the class name from the Java code and then converts that to a JavaScript message. Inspecting the constructor of the new object and extracting its name property tells you what class JavaScript has wrapped around the Java object.

Warnings:

Example code:

   <HTML>
   <HEAD>
   </HEAD>
   <BODY>
   <SCRIPT>
   // Instantiate a Java object
   myJavaString = new java.lang.String;
   // Display its Java class
   document.write("Java class : ");
   document.write(myJavaString.getClass());
   document.write("<BR><BR>");
   // Display its JavaScript class
   document.write("JavaScript class : ");
   document.write(myJavaString.constructor.name);
   document.write("<BR><BR>");
   </SCRIPT>
   </BODY>
   </HTML>

See also:LiveConnect