Using the operator new in object expressions

Gosu uses the new operator to create an instance of a type. The type can be a Gosu class, a Java class, a Guidewire entity type, or an array. Creating a new instance is also called instantiation. Instantiation allocates memory to a new object of that type and initializes the object. After you instantiate an object, you can call object methods or access instance fields and properties.

At least one constructor (creation function) must be exposed on a type to construct an instance of the type with the new operator. You can have multiple constructors with different types and numbers of arguments.

Although creating a new object is typically used as an expression, it can also optionally be a statement if the return value is not needed.

Syntax for typical cases

new javaType (argument_list)          // The optional argument list contains constructor arguments.
new gosuType (argument_list)          // The optional argument list contains constructor arguments.
new arrayType [size]        
new arrayType [] {array_Value_List}   // This syntax allows declaring a list of initial array members.

If you pass arguments to the new operator, Gosu passes those arguments to the constructor. There might be multiple constructors defined, in which case Gosu uses the types and numbers of objects passed as arguments to determine which constructor to call.

Examples

Expression

Result

new java.util.HashMap(8)

Creates an instance of the HashMap Java class.

new String[12]

Creates a String array that has12 members with no initial values.

new String[] {"a", "b", "c"}

Creates a String array that has three members, initialized to "a", "b", and "c".

See also