Documentation

This is machine translation

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

javaMethod

Call Java method

Syntax

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

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.

example

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

Examples

collapse all

Call Method on Java Object

Create a java.util.Date object myDate and change the month to April, which has a numeric value of 3.

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

Call Static Method

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

Call Method Specified at Runtime

This example, searching for a text pattern in a string, uses 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.

javaMethod(fnc,gAddress,str)
ans =

     1

gAddress starts with the words Four score.

Input Arguments

collapse all

MethodName — Name of nonstatic Java methodcharacter vector

Name of nonstatic Java method, specified as a character vector.

Data Types: char

JavaObj — ArrayJava object

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

x1,...,xN — Java method input argumentsany type

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

StaticMethodName — Name of static Java methodcharacter vector

Name of static Java method, specified as a character vector.

Data Types: char

ClassName — Name of Java classcharacter vector

Name of Java class, specified as a character vector, containing StaticMethodName.

Data Types: char

More About

collapse all

Tips

  • Use either MATLAB® or Java syntax to call methods on Java objects:

    % MATLAB syntax
    method(object,arg1,...,argn)
    /* Java syntax */
    object.method(arg1,...,argn)
  • Use javaMethod to call methods having names that exceed the maximum length of a MATLAB identifier. (Call the namelengthmax function to obtain the maximum identifier length.)

    This is the only way you can call such a method in MATLAB. For example, if you have the following function:

    javaMethod('DataDefinitionAndDataManipulationTransactions',T)
    
  • Use javaMethod when you want 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?