
With Release 7.40 ABAP supports so called constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax for constructor expressions is
... operator type( ... ) ...
operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be dreived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be specified.
The instantiation operator NEW is a constructor operator that creates an object (anonymous data object or instance of a class).
creates an anonymous data object of data type dtype and passes a value to the created object. The value construction capabilities cover structures and internal tables (same as those of the VALUE operator).
creates an instance of class class and passes parameters to the instance constructor.
creates either an anonymous data object or an instance of a class depending on the operand type.
You can write a compnent selector -> directly behind NEW type( ... ).
FIELD-SYMBOLS <fS> TYPE data.
DATA dref TYPE REF TO data.
CREATE DATA dref TYPE i.
ASSIGN dref->* TO <fs>.
<fs> = 555.
DATA dref TYPE REF TO data.
dref = NEW i( 555 ).
DATA oref TYPE REF TO class.
CREATE OBJECT oref EXPORTING ...
Either
DATA oref TYPE REF TO class.
oref = NEW #( ... ).
or with an inline declaration
DATA(oref) = NEW class( ... ).
This is the kind of statement NEW is made for. You can also pass it to methods expecting references.
TYPES: BEGIN OF t_struct1,
col1 TYPE i,
col2 TYPE i,
END OF t_struct1,
BEGIN OF t_struct2,
col1 TYPE i,
col2 TYPE t_struct1,
col3 TYPE TABLE OF t_struct1 WITH EMPTY KEY,
END OF t_struct2,
t_itab TYPE TABLE OF t_struct2 WITH EMPTY KEY.
DATA(dref) =
NEW t_itab( ( col1 = 1
col2-col1 = 1
col2-col2 = 2
col3 = VALUE #( ( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ) )
( col1 = 2
col2-col1 = 2
col2-col2 = 4
col3 = VALUE #( ( col1 = 2 col2 = 4 )
( col1 = 6 col2 = 8 ) ) ) ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |