This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / J/Link / Part 1. Installable Java: Calling Java from Mathematica /

1.1.5 Creating Objects

To instantiate Java objects, use the JavaNew function. The first argument to JavaNew is the object's class, specified either as a JavaClass expression returned from LoadJavaClass or as a string giving the fully qualified class name (i.e., having the full package prefix with all the periods). If you wish to supply any arguments to the object's constructor, they follow as a sequence after the class.

Constructing Java objects.

For example, this will create a new Frame:

The return value from JavaNew is a strange expression that looks like it has the head JavaObject, except that it is enclosed in angle brackets. The angle brackets are used to indicate that the form in which the expression is displayed is quite different from its internal representation. These expressions will be referred to as JavaObject expressions. JavaObject expressions are displayed in a way that shows their class name, but you should consider them opaque, meaning that you cannot pick them apart or peer into their insides. You can only use them in J/Link functions that take JavaObject expressions. For example, if obj is a JavaObject, you cannot use First[obj] to get its class name. Instead, there is a J/Link function, ClassName[obj], for this purpose.

JavaNew invokes a Java constructor appropriate for the types of the arguments being passed in, and then returns to Mathematica what is, in effect, a reference to the object. That is how you should think of JavaObject expressions—as references to Java objects very much like object references in the Java language itself. What is returned to Mathematica is not large no matter what type of object you are constructing. In particular, the object's data (that is, its fields) are not sent back to Mathematica. The actual object remains on the Java side, and Mathematica gets a reference to it.

The Frame class has a second constructor, which takes a title in the form of a string. Here is how you would call that constructor:

Note that simply constructing a Frame does not cause it to appear. That requires a separate step (calling the frame's show or setVisible methods will work, but as we will see later, J/Link provides a special function, JavaShow, to make Java windows appear and come to the foreground).

The previous examples specified the class by giving its name as a string. You can also use a JavaClass expression, which is a special expression returned by LoadJavaClass that identifies a class in a particularly efficient manner. When you specify the class name as a string, the class is loaded if it has not already been.

JavaNew is not the only way to get a reference to a Java object in Mathematica. Many methods and fields return objects, and when you call such a method, a JavaObject expression is created. Such objects can be used in the same way as ones you explicitly construct with JavaNew.

At this point, you may be wondering about things like reference counts and how objects returned to Mathematica get cleaned up. These issues are discussed in Section 1.2.3.

J/Link has two other functions for creating Java objects, called MakeJavaObject and MakeJavaExpr. These specialized functions are described in Section 1.2.6.