Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

javaMethod

Call Java method

Syntax

javaMethod(MethodName,JavaObj,x1,...,xN)
javaMethod(StaticMethodName,ClassName,x1,...,xN)

Description

example

javaMethod(MethodName,JavaObj,x1,...,xN) calls the method in the class of the Java® object array with the signature matching the arguments x1,...,xN. Use javaMethod to call methods having names that exceed the maximum length of a MATLAB® identifier. This approach is the only way you can call such a method in MATLAB. To obtain the maximum identifier length, call the namelengthmax function.

In general, use MATLAB syntax to call methods on Java objects.

method(object,arg1,...,argn)

Alternatively, use Java syntax.

object.method(arg1,...,argn)

example

javaMethod(StaticMethodName,ClassName,x1,...,xN) calls the static method in class ClassName.

In general, use MATLAB syntax to call static methods on Java objects.

class.method(arg1,...,argn)

Examples

collapse all

Create a java.util.Date object myDate and change the month to April. From the Java documentation, "A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth." Therefore, the numeric value for April is 3.

myDate = java.util.Date;
javaMethod('setMonth',myDate,3)

Call java.lang.Double static method isNaN to test variable num. Since num contains a number, no message is displayed.

num = 2.2;
if javaMethod('isNaN','java.lang.Double',num)
    disp('This is not a number')
end

Search for a text pattern in a string using variables for the pattern and for the search method. These variables could be set at runtime from user input.

Choose method, startsWith, and identify pattern, str.

fnc = 'startsWith';
str = java.lang.String('Four score');

Identify text to search.

gAddress = java.lang.String('Four score and seven years ago');

Search gAddress for the pattern.

javaMethod(fnc,gAddress,str)
ans = logical
   1

gAddress starts with the words Four score.

Call the constructor of or a static method in an inner class. In the javaMethod and javaObject functions, specify the class name, using the $ character, as OuterClass$InnerClass.

For example, suppose class com.ams.MyClass contains class MyInnerClass with static method methodname. In Java, the calling syntax is:

out = com.ams.MyClass.MyInnerClass.methodname(arg);

In MATLAB, type:

out = javaMethod('methodname','com.ams.MyClass$MyInnerClass',arg)

Input Arguments

collapse all

Nonstatic Java method name, specified as a string or character vector.

Example: javaMethod('DataDefinitionAndDataManipulationTransactions',T)

Data Types: char | string

Array, specified as a Java object of the class containing the method.

Java method input arguments, 1 through N (if any), required by MethodName or StaticMethodName, specified by any type. The method argument list specifies the argument type.

Static Java method name, specified as a string or character vector.

Example: java.lang.Double.isNaN(2.2)

Data Types: char | string

Java class name, specified as a string or character vector, containing StaticMethodName.

Data Types: char | string

Tips

  • Use javaMethod to specify the method name as a variable to be invoked at runtime. When calling a static method, you also can use a variable in place of the class name argument. For example, see Call Method Specified at Runtime.

Introduced before R2006a

Was this topic helpful?