SAP CONSTRUCTOR EXPRESSION NEW

Get Example source ABAP code based on a different SAP table
  


ARTICLE
• NEW ABAP_OPERATOR
• # NEW

NEW - Instance Operator

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

Effect
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 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 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 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 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,
It is possible to introduce standalone method calls including chained method calls.
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.
The # character.


ABAP Code Snippet
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.
Initial value for all types
Initial value for all data types
DS:ABEN.NEW_CONSTRUCTOR_PARAMS_DT>Datums-/Zeittypen
Structures
Internal tables
Classes

ABAP_SUBRC_GENERAL
If an instance of a class is created successfully, the instance operator NEW sets sy-subrc to 0. Non-class-based exceptions of the instance constructor cannot be handled, which means that 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.

Note
From a technical perspective, the instance operator NEW 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.

Example
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 oref). CLASS class DEFINITION.
...
ENDCLASS.

DATA: dref TYPE REF TO data,
oref TYPE REF TO class.

dref = NEW i( 555 ).
oref = NEW #( ).