The Java® programming language is used to write COMSOL methods, which means that Java
® statements and syntax in general can be used. This section contains simple examples of some of the most common language elements. For more information and examples, see the
Application Programming Guide and the
Programming Reference Manual.
Variables defined in the Declarations node are available as global variables in a method and need no further declarations.
The with command is specific to the COMSOL API and not part of the standard Java
® programming language.
You would typically use the Editor Tools window for generating code for setting the value of a global parameter. While in the Method Editor, right-click the parameter and select
Set.
To get the global parameter L and store it in a double variable
Length:
Multiply the value of the variable Length with
2 and write the result to the parameter
L including the unit of
cm.
Comparing string values in Java® has to be done with
.equals() and not with the
== operator. This is due to the fact that the
== operator compares whether the strings are the same objects and does not consider their values. The below code demonstrates string comparisons:
The methods alert,
confirm, and
request display a dialog with a text string and optional user input. The following example uses
confirm to ask the user if a direct or an iterative solver should be used in an application. Based on the answer, the
alert function is then used to show the estimated memory requirement for the selected solver type in a message dialog: