External Interfaces Previous page   Next Page

Creating and Using Java Objects

In MATLAB, you create a Java object by calling one of the constructors of that class. You then use commands and programming statements to perform operations on these objects. You can also save your Java objects to a MAT-file and, in subsequent sessions, reload them into MATLAB.

This section addresses the following topics:

Constructing Java Objects

You construct Java objects in MATLAB by calling the Java class constructor, which has the same name as the class. For example, the following constructor creates a Frame object with the title 'Frame A' and the other properties with their default values.

Displaying the new object frame shows the following.

All of the programming examples in this chapter contain Java object constructors. For example, the sample code for Reading a URL creates a java.net.URL object with the constructor

Using the javaObject Function

Under certain circumstances, you may need to use the javaObject function to construct a Java object. The following syntax invokes the Java constructor for class, class_name, with the argument list that matches x1,...,xn, and returns a new object, J.

For example, to construct and return a Java object of class java.lang.String, you use

Using the javaObject function enables you to:

The default MATLAB constructor syntax requires that no segment of the input class name be longer than namelengthmax characters. (A class name segment is any portion of the class name before, between, or after a dot. For example, there are three segments in class, java.lang.String.) Any class name segment that exceeds namelengthmax characters is truncated by MATLAB. In the rare case where you need to use a class name of this length, you must use javaObject to instantiate the class.

The javaObject function also allows you to specify the Java class for the object being constructed at run-time. In this situation, you call javaObject with a string variable in place of the class name argument.

In the usual case, when the class to instantiate is known at development time, it is more convenient to use the MATLAB constructor syntax. For example, to create a java.lang.String object, you would use

Java Objects Are References in MATLAB

In MATLAB, Java objects are references and do not adhere to MATLAB copy-on-assignment and pass-by-value rules. For example,

In the third statement above, the variable newFrameRef is a second reference to origFrame, not a copy of the object. In any code following the example above, any change to the object at newFrameRef also changes the object at origFrame. This effect occurs whether the object is changed by MATLAB code, or by Java code.

The following example shows that origFrame and newFrameRef are both references to the same entity. When the size of the frame is changed via one reference (newFrameRef), the change is reflected through the other reference (origFrame), as well.


Previous page  Locating Native Method Libraries Concatenating Java Objects Next page

© 1994-2005 The MathWorks, Inc.