new

The 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:

  • getAnInt returns an Integer object.

  • getAnArray returns a two-dimensional array with lengths equal to dim1 and dim2.

  • getanInitializedArray returns a two-dimensional array. The first dimension is itself an array of length 3, that is, the number of braced elements in the initialValues list.

Each array consists of an Integer array:

  1. The first one is an array of length 2, initialized with the Integer values 1 and 2.

  2. The second one is an array of length 2, initialized with the values 3 and 4.

  3. The last one is an array of length 3, initialized with the values 5, 6, and 7.