External Interfaces Previous page   Next Page

Invoking Methods on Java Objects

This section explains how to invoke an object's methods in MATLAB. It also covers how to obtain information related to the methods that you're using and how MATLAB handles certain types of nonstandard situations.

This section addresses the following topics:

Using Java and MATLAB Calling Syntax

To call methods on Java objects, you can use the Java syntax

In the following example, frame is the java.awt.Frame object created above, and getTitle and setTitle are methods of that object.

Alternatively, you can call Java object (nonstatic) methods with the MATLAB syntax

With MATLAB syntax, the java.awt.Frame example above becomes

All of the programming examples in this chapter contain invocations of Java object methods. For example, the code for Reading a URL contains a call, using MATLAB syntax, to the openStream method on a java.net.URL object, url.

In another example, the code for Example - Creating and Using a Phone Book contains a call, using Java syntax, to the load method on a java.utils.Properties object, pb_htable.

Using the javaMethod Function on Nonstatic Methods

Under certain circumstances, you may need to use the javaMethod function to call a Java method. The following syntax invokes the method, method_name, on Java object J with the argument list that matches x1,...,xn. This returns the value X.

For example, to call the startsWith method on a java.lang.String object passing one argument, use

Using the javaMethod function enables you to

The only way to invoke a method whose name is longer than namelengthmax characters is to use javaMethod. The Java and MATLAB calling syntax does not accept method names of this length.

With javaMethod, you can also specify the method to be invoked at run-time. In this situation, your code calls javaMethod with a string variable in place of the method_name argument. When you use javaMethod to invoke a static method, you can also use a string variable in place of the class name argument.


Previous page  Determining the Class of an Object Invoking Static Methods on Java Classes Next page

© 1994-2005 The MathWorks, Inc.