# Calling Static Methods in a Java Class

Static methods can be called directly. The localname of the function must match the name of a public static method in this class. The names match if they contain the same characters, excluding hyphens and forcing any character that follows a hyphen to upper-case. For example the XPath function call `to-string()` matches the Java method `toString()`; but the function call can also be written as `toString()` if you prefer.

If there are several methods in the class that match the localname, and that have the correct number of arguments, then the system attempts to find the one that is the best fit to the types of the supplied arguments: for example if the call is `f(1,2)` then a method with two `int` arguments will be preferred to one with two `float` arguments. The rules for deciding between methods are quite complex. Essentially, for each candidate method, Saxon calculates the "distance" between the types of the supplied arguments and the Java class of the corresponding method in the method's signature, using a set of tables given below. For example, the distance between the XPath data type "xs:Integer" and the Java class "long" is very small, while the distance between an XPath xs:integer and a Java boolean is much larger. If there is one candidate method where the distances of all arguments are less-than-or-equal-to the distances computed for other candidate methods, and the distance of at least one argument is smaller, then that method is chosen. If there are several methods with the same name and the correct number of arguments, but none is preferable to the others under these rules, an error is reported: the message indicates that there is more than one method that matches the function call.

This binding is carried out statically, using the static types of the supplied arguments, not the dynamic types obtained when the arguments are evaluated. If there is insufficient static type information to distinguish the candidate methods, an error is reported. You can supply additional type information using the `treat as` expression (or by casting).

For example (in XSLT):

    <xsl:value-of select="math:sqrt($arg)"
       xmlns:math="java:java.lang.Math"/>

This will invoke the static method `java.lang.Math#sqrt()`, applying it to the value of the variable `$arg`, and copying the value of the square root of `$arg` to the result tree.

Similarly (in XQuery):

    <a xmlns:double="java:java.lang.Double"/>
                                  {double:MAX_VALUE()} </a>

This will output the value of the static field `java.lang.Double#MAX_VALUE`. (In practice, it is better to declare the namespace in the query prolog, because it will then not be copied to the result tree.)

Expand | Up Next