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 and assigns
values to the new object. 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 extended 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. Using an object component selector
->, a constructor expression of this type can be extended in both general expression positions and functional positions (like an object reference variable) and can be used in the same operand positions. The following is also possible:
- A single expression that points to an attribute of the class using exactly one follow-on 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 known in full, 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 to the
CREATE statements. Once an object is
created, it is provided with values using the parameters in parentheses. The syntax used in parameter passing depends on the type used. There are specialized categories of parameter passing 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.
- To create the values for a new data object, the instance operator
NEWuses mainly the same syntax as the value operator
- 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 statement is closed or after the analysis of a relational expression once the truth value is 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
cls (derived implicitly from the static type of
oref). In this case, the last statement could be written just as explicitly as oref = NEW cls(
) or it could be written as
DATA(oref) = NEW cls( ), using an inline declaration instead of the preceding
CLASS cls DEFINITION. ... ENDCLASS. DATA: dref TYPE REF TO data, oref TYPE REF TO cls. dref = NEW i( 555 ). oref = NEW #( ).