ABAP News for Release 7.40 – Constructor Operator NEW
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.
Instantiation Operator NEW
The instantiation operator NEW is a constructor operator that creates an object (anonymous data object or instance of a class).
- … NEW dtype( value ) …
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).
- … NEW class( p1 = a1 p2 = a2 … ) …
creates an instance of class class and passes parameters to the instance constructor.
- … NEW #( … ) …
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( … ).
Example for data objects
Before Release 7.40
FIELD-SYMBOLS <fS> TYPE data.
DATA dref TYPE REF TO data.
CREATE DATA dref TYPE i.
ASSIGN dref->* TO <fs>.
<fs> = 555.
With Release 7.40
DATA dref TYPE REF TO data.
dref = NEW i( 555 ).
Example for instances of classes
Before Release 7.40
DATA oref TYPE REF TO class.
CREATE OBJECT oref EXPORTING …
With Release 7.40
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.
And one for the road …
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 ) ) ) ).
Hi again Horst,
I think it's a very good idea you are releasing these new stuff in daily-sh blogs. Otherwise my brain would melt. Keep them coming!
Cheers,
Custodio
Hi Horst Keller,
nice piece of information. It will reduce the lines of code and also make using OOPs concepts easier.
Thanks for sharing the info,
Cheers,
Manu B
Hello Horst,
I have bin awaiting this since I saw the slide in the NW 7.4 announcement. Please allow me to ask...
thanks for taking the time.
JNN
Hi Horst,
thanks for sharing this. I really like the new features. Comming from other programming languages, I'm happy to see these things like the NEW operator finally being available in ABAP, too. Can't wait to have a 7.40 somewhere to try out these new features, also the new expression posibilities.
Cheers,
Marc
Hi Horst,
Thanks a lot, for the info.
Well, ABAP looks very much similar to Java now.
Just wanted to know one thing,
Since we will be using.
dref = NEW i( 555 ).
Can we expect one special class for Integer and other data types (some sort of Wrapper Class ), as we have in couple of other OOPS supporting languages ?
Thanks a lot again.
I am loving it ....!!!!!!!!! 🙂 😀
Hi Ankit,
No, this is not planned. In ABAP (as a 4GL language) we will stay with the concept of data types and data objects as instances of data types on the one hand side and with classes and objects as instances of classes at the other hand side (where classes and data are in the same namespace).
With the type concept, you can create complex types (structures and tables) and you have ABAP statements and expressions (that play the role of library methods from other languages) working with data objects of these types. Classes are "modularization" units for (business) implementations that use these statements for working with data objects.
Hi Horst,
Thanks a lot, well.... can you please reveal your thoughts about ABAP Destrucotr.....!!!!!!
As per OSS : 168703,
" In light of all these problems, the designers of ABAP Objects decided no not provide an ABAP Destructor at this time. Instead, there is a provision for a 'C-destructor' that can however only be used by SAP kernel developers".
Thanking You All.
Hi Ankit,
ABAP Objects does not provide any destructors for application development for several reasons (performance, transaction concept, etc.),
These reasons are explained in OSS : 168703.
My thoughts? Well, I placed 168703 in OSS 😉
Thanks for providing the insight Horst 🙂
Hi,
is it possible to specify the type dynamically like with create object?
e.g.
create object lo_obj type (lv_classname) exporting param_1 = lv_param_1?
lo_obj ?= new (lv_classname)( lv_param_1 = lv_param_1) is not working...
Thanks,
Hannes
Hello,
No, this is not possible. For all constructor operators the type must be known at compile time, eihter given directly or inferred from the operand position.
Best
Horst
Hi,
is this feature planned for upcoming service packs or for the new NW 750?
Cheers
Christian
I'm not authorized to make statements about upcoming releases, sorry ...
Hi Horst ,
Nice Blog... good to learn new things ....
What is the exact difference between NEW and VALUE Constructor Operators ....
i thought Both are same and behaves same ...
Thanks & Regards ,
Vamsilakshman
Hi,
NEW creates an object like CREATE OBJECT and a value, VALUES creates only a value. NEW returns a reference to the object created. VALUE retrurns a value.
Horst
Thanks for the reply Horst ,
You mean to say , NEW Operator is Only for OOPs Concept ..
Instead of Instantiate the Reference Object for a class we use NEW Operator ...In one statement...
Vamsi
NEW can also replace CREATE DATA.
Horst
Thanks Horst ,
Vamsi
Hi Horst,
Can NEW be used with a type handle(from RTT) or if not currently are there any plans to make this possible?
Something like...
Before ...
CREATE DATA dref TYPE HANDLE lr_data_descr.
With ...
dref = NEW lr_data_descr( ).
Or even ...
dref = NEW cl_abap_tabledescr=>create(...)( ).
Hi Thomas,
as for all constructor expressions the data type must be known at compile time. Otherwise, the expression cannot be evaluated. E.g. how would it be possible to pass parameters in the brackets if the type would be generic or even dynamic.
Best
Horst
How can we use dynamic types with NEW like we use them with CREATE DATA?
CREATE DATA dref TYPE (type).
As documented, not.
And is it planned in future releases? It is quite logical step concerning NEW is a modern way of creating data elements.
I can't answer that question.
Hi Horst,
Can you advice on when to use NEW and when to use VALUE?
What are the advantages of using NEW operator over VALUE operator?
Regards,
Sid
About the same difference as between CREATE DATA and DATA VALUE.
Hi!
Like always a great article - thank you!
I have only one question - from the first example - shouldn't be there: CRATE OBJECT instead of CREATE DATA dref TYPE i.
Best regards
Paweł
how replace this :
DATA:   lo_smem_obj TYPE REF TO zsm_nwp1_objects,
lo_root_obj TYPE REF TO zcl_shared_mem_for_nwp1.
lo_smem_obj = zsm_nwp1_objects=>attach_for_write( ).
CREATE OBJECT lo_root_obj AREA HANDLE lo_smem_obj. <----- is this line could be written with the new syntax ?
No,
There is no parameter AREA HANDLE for the NEW constructor.