To call methods on Java® objects, you can use either Java calling
syntax or MATLAB® calling syntax. Under certain circumstances,
use the MATLAB
To call methods on Java objects, use the Java syntax:
To call methods on Java objects, use the MATLAB syntax:
For example, to call the
using MATLAB syntax:
mlDate = java.util.Date; setHours(mlDate,3) getHours(mlDate)
ans = 3
The MATLAB syntax is the preferred syntax for invoking
a Java method. Use
Use the MATLAB
Use Java methods with names that exceed the maximum
length of a MATLAB identifier. (Call the
to obtain the maximum identifier length.)
Specify a Java method to invoke at run time.
For example, your code calls
a character vector variable in place of the
When you use
javaMethod to invoke a static method,
you also can use a character vector variable in place of the class
Call the constructor of or a static method in an inner
class. In the
specify the class name, using the
For example, suppose class
MyInnerClass with static method
In Java, the calling syntax is:
out = com.ams.MyClass.MyInnerClass.methodname(arg);
In MATLAB, type:
out = javaMethod('methodname','com.ams.MyClass$MyInnerClass',arg)
To invoke a static method on a Java class, use the Java syntax:
For example, call the static method,
ans = 0
MATLAB provides the
to obtain information about the Java methods you are using. You
also can request a listing of every Java class that you loaded
into MATLAB that implements a specified method.
methodsto Display Method Names and Arguments
methods function returns information
on methods of MATLAB and Java classes.
To return the names of all the methods (including inherited
methods) of the class, use
methods without the
Names of overloaded methods are listed only once.
a listing of the method names (including inherited methods) along
with attributes, argument lists, and inheritance information on each.
Each overloaded method is listed separately.
For example, display a full description of all methods of the
methods java.awt.Dimension -full
Methods for class java.awt.Dimension: Dimension() Dimension(java.awt.Dimension) Dimension(int,int) java.lang.Class getClass() % Inherited from java.lang.Object int hashCode() % Inherited from java.lang.Object boolean equals(java.lang.Object) java.lang.String toString() void notify() % Inherited from java.lang.Object void notifyAll() % Inherited from java.lang.Object void wait(long) throws java.lang.InterruptedException % Inherited from java.lang.Object void wait(long,int) throws java.lang.InterruptedException % Inherited from java.lang.Object void wait() throws java.lang.InterruptedException % Inherited from java.lang.Object java.awt.Dimension getSize() void setSize(java.awt.Dimension) void setSize(int,int)
methodsviewto Display Argument Types and Exceptions
To see methods implemented by a particular Java (or MATLAB)
class, use the
methodsview function. Specify
the class name (along with its package name, for Java classes)
in the command line. If you have imported the package that defines
this class, then the class name alone suffices.
The following command lists information on all methods in the
A new window appears, listing one row of information for each method in the class.
Each row in the window displays up to six fields of information
describing the method. The following table lists the fields displayed
methodsview window along with a description
and examples of each field type.
Fields Displayed in methodsview Window
Method type qualifiers
Type returned by the method
Types of arguments passed to method
Other relevant information
Parent of the specified class
whichto Determine What Classes Define a Method
which function to display the fully
qualified name (package and class name) of a method implemented by
a loaded Java class. To find all classes
that define the specified method, use the
For example, suppose you want to find the package and class
name for the
concat method. Type:
java.lang.String class is loaded, MATLAB displays:
concat is a Java method % java.lang.String method
String class has not been loaded, MATLAB displays:
concat not found.
Suppose that you loaded the Java
Both of these classes have an
equals method. Type:
which -all equals
The MATLAB display includes entries like the following:
equals is a Java method % java.lang.String method equals is a Java method % java.awt.Frame.equals equals is a Java method % com.mathworks.jmi.MatlabPath method
which function operates differently
on Java classes than it does on MATLAB classes.
displays MATLAB classes, whether they are loaded.
displays Java classes that are loaded. You can find out which Java classes
are currently loaded by using the command
described in Determining Which Classes Are Loaded.
For a description of how Java classes are loaded, see Making Java Classes Available in MATLAB Workspace.
MATLAB commands that operate on Java objects and arrays use the methods that are implemented within, or inherited by, these objects' classes. There are some MATLAB commands that you can alter in behavior by changing the Java methods that they use.
You are calling the
disp function when
Display the value of a variable or an expression in MATLAB.
Terminate a command line without a semicolon.
Display a Java object in MATLAB.
disp on a Java object, MATLAB formats
the output using the object
toString method. If
the class does not implement this method, then MATLAB uses an
toString method. If no intermediate ancestor
classes define this method, MATLAB uses the
defined by the
To change the way MATLAB displays an object, implement
toString method in your class definition.
isequal function compares
two or more arrays for equality in type, size, and contents. Also,
you can use this function to test Java objects for equality.
When you compare two Java objects using
isequal, MATLAB performs
the comparison using the Java method,
equals. MATLAB first
determines the class of the objects specified in the command, and
then uses the
equals method implemented by that
equals is not implemented in this class,
then MATLAB uses an inherited
If no intermediate ancestor classes define this method, MATLAB uses
equals method defined by the
To change the way MATLAB compares members of a class, implement
equals method in your class definition.
You can change the output of the MATLAB
by defining your own Java methods,
For more information, see Converting to the MATLAB double Type and Converting to the MATLAB char Type.
If your MATLAB command invokes a nonexistent method on a Java object, MATLAB looks for a function with the same name. If MATLAB finds a function of that name, it attempts to invoke it. If MATLAB does not find a function with that name, it displays a message stating that it cannot find a method by that name for the class.
For example, MATLAB has a function named
and the Java API
java.awt.Frame class also
size method. If you call
Frame object, the
java.awt.Frame is executed. However,
if you call
size on an object of
java.lang.String, MATLAB does
not find a
size method for this class. It executes
size function instead.
text = java.lang.String('hello'); size(text)
ans = 1 1
Note: When you define a Java class for use in MATLAB, avoid giving any of its methods the same name as a MATLAB function.
to handle Java exceptions.
When calling a
main method from MATLAB,
the method returns when it executes its last statement, even if the
method creates a thread that is still executing. In other environments,
main method does not return until the thread
You, therefore, be cautious when calling
from MATLAB, particularly
main methods that
start a user interface.
main methods are written
assuming they are the entry point to application code. When called
from MATLAB this is not the case, and the fact that other Java UI
code might be already running can lead to problems.