new
new keyword creates a new object or
array.
Purpose
This allocation expression is used in the action part of a rule or function to create a new object.
Context
Functions or rule actions
Syntax
new className
(
arguments
) ;
new className
[
dimension
] [
dimension
]* { initialValues
} ;
Description
In the case of arrays, you can
specify how to initialize it with the initialValues method.
Example
function Integer getAnInt(int value)
{
return new Integer(value);
}
function Integer[][] getAnArray(int dim1, int dim2)
{
return new Integer[dim1][dim2];
}
function Integer[][] getAnInitializedArray()
{
int[][] array2 = {{1,2}, {3,4}, {5,6,7}};
return array2;
}This example consists of three functions:
getAnIntreturns an Integer object.getAnArrayreturns a two-dimensional array with lengths equal todim1anddim2.getanInitializedArrayreturns a two-dimensional array. The first dimension is itself an array of length 3, that is, the number of braced elements in theinitialValueslist.
Each array consists of an Integer array:
The first one is an array of length 2, initialized with the Integer values 1 and 2.
The second one is an array of length 2, initialized with the values 3 and 4.
The last one is an array of length 3, initialized with the values 5, 6, and 7.