NEW - Instance Operator
7.31 | 7.40 | 7.54
... NEW type( ... ) ...
A constructor expression with the instance operator
NEW creates an
anonymous data object or an
instance of a class. The result
is a reference variable that points to the new object. The following can be specified for
- A non-generic data type
NEWworks in the same way as the statement CREATE DATA dref TYPE dtype, where
drefstands for the result that points to the new anonymous data object. The result is a data reference variable of the static type
dtype. A constructor expression of this type cannot be continued using a component selector.
- A class
NEWworks in the same way as the statement CREATE OBJECT oref TYPE class, where
orefstands for the result that points to the new object. The result is an object reference variable of the static type
class. A constructor expression of this type can be continued at general expression positions and functional positions, like an object reference variable, using an object component selector
->, and can be used at the same operand positions. Furthermore,
- A single expression, which points to an attribute of the class using exactly one following object component selector, can also be used as the target field of assignments.
If the data type required in an operand position is unique and fully recognizable, the
#character can be used instead of an explicitly specified type
typeand the operand type is used. If the operand type is not fully identifiable, the character
#cannot be used, with the following exception: The operand can be evaluated after
BASEwhen a structure or an internal table is constructed.
The same descriptions apply as for the
CREATE statements. Once an object
has been created, it is provided with values using the parameters in parentheses. The syntax used in pass by parameter depends on the type used. There are specialized categories of pass by parameter for complex types.
When a constructor expression is assigned to a reference
NEW, the information in the parentheses is evaluated before the new object is bound to the target variable.
- The instance operator
NEWalways creates a new temporary reference variable that points to the new object. The reference variable is used as the operand of a statement and then deleted. It is deleted when the current statements is closed or after the analysis of a relational expression once the logical value has been determined. The new object is passed to the garbage collector if it is not passed to a heap reference or a field symbol after the temporary reference variable is deleted.
- Assignments to a reference variable also always
create a temporary reference variable that is only assigned to the target variable afterwards. This
means that the object pointed to by the target variable before the assignment can be addressed using
it throughout the entire expression. This is the difference between
NEWand the value operator
Creates an anonymous data object of the type
i with the value 555 and an
instance of a local class
class (derived implicitly from the static type of
CLASS class DEFINITION.
DATA: dref TYPE REF TO data,
oref TYPE REF TO class.
dref = NEW i( 555 ).
oref = NEW #( ).