Ansicht
Dokumentation

ABENCONSTRUCTOR_EXPRESSION_NEW - CONSTRUCTOR EXPRESSION NEW

ABENCONSTRUCTOR_EXPRESSION_NEW - CONSTRUCTOR EXPRESSION NEW

Fill RESBD Structure from EBP Component Structure   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.
SAP E-Book

NEW, Instance Operator

... NEW type( ... ) ...

Effect

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 created object. The following can be specified for type:

  • A non-generic data type dtype.
The operator NEW works in the same way as the statement CREATE DATA dref TYPE dtype, where dref stands for the result that points to the created 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 class.
The operator NEW works in the same way as the statement CREATE OBJECT oref TYPE class, where oref stands for the result that points to the created object. The result is an object reference variable of the static type class. In general expression positions and functional positions, a component selector can be added to such a constructor expression, like an object reference variable with an component selector -> 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 subsequent object component selector can also be used as the target field of assignments.

  • The # character.
If the operand type is not known completely, the # character cannot be used, with the following exception: The operand can be evaluated after BASE when a structure or an internal table is constructed.

The same descriptions apply as to the CREATE statements. After an object is created successfully, it is supplied with values using the parameters specified in parentheses. The syntax of the parameter passing depends on the type used. There are specialized types of parameter passing for complex types.

If a constructor expression is assigned to a reference variable using NEW, the information in the parentheses is evaluated before the created object is bound to the target variable.

Return Value

If an instance of a class is created successfully, the instance operator NEW sets sy-subrc to 0. Since non-class-based exceptions of the instance constructor cannot be handled, sy-subrc is never set to a value other than 0. The return code sy-subrc is not set when anonymous data objects are created.

Notes

  • To create the values for a new data object, the instance operator NEW uses mainly the same syntax as the value operator VALUE.
  • The instance operator NEW always creates a new temporary reference variable that points to the created 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 evaluation of a relational expression once the truth value is determined. The created object is passed to the Garbage Collector if it is not received by a heap reference or a field symbol after the temporary reference variable has been 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 to which the target variable points before the assignment can be addressed using it throughout the entire expression. This is the difference between NEW and the value operator VALUE.

Example

Creation of an anonymous data object of the type i with the value 555 and an instance of a local class cls whereby this is derived implicitly from the static type of oref. In this case, the last statement could be written explicitly as oref = NEW cls( ) or as DATA(oref) = NEW cls( ), using an inline declaration instead of the preceding DATA statement.






Addresses (Business Address Services)   ABAP Short Reference  
This documentation is copyright by SAP AG.

Length: 6940 Date: 20240426 Time: 123654     sap01-206 ( 112 ms )