ABAP Keyword Documentation → ABAP - Reference → Creating Objects and Values → NEW - Instance Operator
NEW - Single Value for All Data Types
Other versions:
7.31 | 7.40 | 7.54
Syntax
... NEW dtype|#( [let_exp] dobj ) ...
Effect
If dtype
is a non-generic elementary data type, a structured type, a table
type, or a reference type or #
represents a type like this, a single data
object dobj
can be specified as a unnamed argument, of the type dtype
or convertible to this type. dobj
is a
general expression
position. The value of dobj
is assigned to the new anonymous data object. The assignment is made in accordance with the applicable
assignment rules. Optionally, a LET
expression let_exp
can be specified before the data object to define local auxiliary fields.
Notes
- In particular, an expression specified for
dobj
can itself be a constructor expression or contain a constructor expression.
- When a constructor expression is to assigned to a reference variable using
NEW
, this variable points to the initial object from the start. This means that the original reference is no longer available using the target variable in the expression.
Example
Constructs an anonymous data object of the type string
, whose value is determined using a string expression.
PARAMETERS input TYPE c LENGTH 10 LOWER CASE DEFAULT 'World'.
DATA dref TYPE REF TO string.
dref = NEW string( `Hello ` && ` ` && input && `!` ).
MESSAGE dref->* TYPE 'I'.
Example
For each row read, a structured anonymous data object is created in a SELECT
loop. The content of the row is assigned to this data object. The object is created in the general expression
position of the statement APPEND
and the new data reference is appended directly
to an internal table with the appropriate row type. The result is a table that references all new anonymous data objects.
DATA dref_tab LIKE TABLE OF REF TO t100 WITH EMPTY KEY.
DATA wa TYPE t100.
SELECT *
FROM t100
WHERE sprsl = @sy-langu
INTO @wa.
APPEND NEW #( wa ) TO dref_tab.
ENDSELECT.