|On this page…|
To call methods on Java® objects, you can use either Java calling syntax or MATLAB® calling syntax. Under certain circumstances, use the MATLAB javaMethod function.
The Java syntax to call methods on Java objects is:
For example, to call the getHours and setHours methods of a java.util.Date object:
myDate = java.util.Date; myDate.setHours(3) myDate.getHours
ans = 3
The MATLAB syntax to call methods on Java objects is:
For example, to call the getHours and setHours methods using MATLAB syntax:
mlDate = java.util.Date; setHours(mlDate,3) getHours(mlDate)
ans = 3
The MATLAB javaMethod function enables you to:
Use Java methods with names that exceed the maximum length of a MATLAB identifier. (Call the namelengthmax function to obtain the maximum identifier length.)
Specify a Java method to invoke at run time.
For example, your code calls javaMethod with a string variable in place of the method argument. When you use javaMethod to invoke a static method, you also can use a string variable in place of the class name argument.
Note: The MATLAB syntax is the preferred syntax for invoking a Java method. Use javaMethod for these special cases only.
To invoke a static method on a Java class, use the Java syntax:
For example, call the static method, isNaN:
ans = 0
MATLAB provides the methods and methodsview functions 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.
The methods function returns information on methods of MATLAB and Java classes.
Use methods without the '-full' qualifier to return the names of all the methods (including inherited methods) of the class. Names of overloaded methods are listed only once.
With the '-full' qualifier, methods returns 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 java.awt.Dimension object.
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)
If you want to know what methods are 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 java.awt.MenuItem class. Type:
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 in the 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
You can use the which function to display the fully qualified name (package and class name) of a method implemented by a loaded Java class. With the -all qualifier, the which function finds all classes with a method of the name specified.
Suppose, for example, that you want to find the package and class name for the concat method, with the String class currently loaded. Use the command:
which concat java.lang.String.concat % String method
If the java.lang.String class has not been loaded, the same which command would give the output:
which concat concat not found.
If you use which -all for the method equals, with the String and java.awt.Frame classes loaded, you see the following display.
which -all equals java.lang.String.equals % String method java.awt.Frame.equals % Frame method com.mathworks.ide.desktop.MLDesktop.equals % MLDesktop method
The which function operates differently on Java classes than it does on MATLAB classes. MATLAB classes are always displayed by which, whether or not they are loaded. This is not true for Java classes. You can find out which Java classes are currently loaded by using the command [m,x,j]=inmem, 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 make use of the methods that are implemented within, or inherited by, these objects' classes. There are some MATLAB commands that you can alter somewhat in behavior by changing the Java methods that they rely on.
You can use the disp function to display the value of a variable or an expression in MATLAB. Terminating a command line without a semicolon also calls the disp function. You also can use disp to display a Java object in MATLAB.
When disp operates on a Java object, MATLAB formats the output using the toString method of the class to which the object belongs. If the class does not implement this method, then an inherited toString method is used. If no intermediate ancestor classes define this method, it uses the toString method defined by the java.lang.Object class. You can override inherited toString methods in classes that you create by implementing such a method within your class definition. In this way, you can change the way MATLAB displays information regarding the objects of the class.
The MATLAB isequal function compares two or more arrays for equality in type, size, and contents. This function also can be used 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 class. If it is not implemented in this class, then an inherited equals method is used. This is the equals method defined by the java.lang.Object class if no intermediate ancestor classes define this method.
You can override inherited equals methods in classes that you create by implementing such a method within your class definition. In this way, you can change the way MATLAB performs comparison of the members of this class.
You also can define your own Java methods toDouble and toChar to change the output of the MATLAB double and char functions. 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 size, and the Java API java.awt.Frame class also has a size method. If you call size on a Frame object, the size method defined by 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 the MATLAB size function instead.
string = java.lang.String('hello'); size(string)
ans = 1 1
Use the matlab.exception.JavaException class to handle Java exceptions.
When calling a main method from MATLAB, the method returns as soon as it executes its last statement, even if the method creates a thread that is still executing. In other environments, the main method does not return until the thread completes execution.
You, therefore, need to be cautious when calling main methods from MATLAB, particularly main methods that launch GUIs. main methods are usually 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 GUI code might be already running can lead to problems.