Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

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 ) ) ) ).

31 Comments
custodio_deoliveira
Active Contributor
0 Kudos

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

former_member209920
Active Participant
0 Kudos

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

nomssi
Active Contributor
0 Kudos

Hello Horst,

I have bin awaiting this since I saw the slide in the NW 7.4 announcement. Please allow me to ask...

  1. type inference: DATA(text) = `...` creates a string, but DATA(text) = '...' will not work, won't it?
  2. are there more operators than NEW and VALUE ?
  3. what is this WITH EMPTY KEY, is it new in 7.4?

thanks for taking the time.

JNN

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
  1. Why not?  DATA(text) = 'aaa' gives a field of type c length 3.
  2. NEW|VALUE|CONV|CAST|REF|EXACT|COND|SWITCH
  3. Finally, someone asked. Yes, new in 7.40. Explicitly declaring an empty key for standard tables. Better than a generic key or the default key in many cases. I''ll come back to that later.
marc_augustin2
Active Participant
0 Kudos

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

Former Member
0 Kudos

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 ....!!!!!!!!!  :smile: :grin:

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ankit,

Can we expect one special class for Integer and other data types (some sort of Wrapper Class )

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.

Former Member
0 Kudos

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.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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 :wink:

SuhaSaha
Advisor
Advisor
0 Kudos

Thanks for providing the insight Horst :smile:

Hal64
Explorer
0 Kudos

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

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

christianlechne
Active Contributor
0 Kudos

Hi,

is this feature planned for upcoming service packs or for the new NW 750?

Cheers

Christian

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

I'm not authorized to make statements about upcoming releases, sorry ...

vamsilakshman_pendurti
Active Participant
0 Kudos

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

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

vamsilakshman_pendurti
Active Participant
0 Kudos

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 

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

NEW can also replace CREATE DATA.

Horst

vamsilakshman_pendurti
Active Participant
0 Kudos

Thanks Horst ,

Vamsi

0 Kudos

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(...)( ).



horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

Astashonok
Participant
0 Kudos
How can we use dynamic types with NEW like we use them with CREATE DATA?

CREATE DATA dref TYPE (type).
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
As documented, not.

 
Astashonok
Participant
0 Kudos
And is it planned in future releases? It is quite logical step concerning NEW is a modern way of creating data elements.
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
I can't answer that question.
Siddharth_Shah
Explorer
0 Kudos
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
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
About the same difference as between CREATE DATA and DATA VALUE.
pablooo860
Explorer
0 Kudos
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ł
herzelhaimharel_gilor
Participant
0 Kudos
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 ?
horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
No,

There is no parameter AREA HANDLE for the NEW constructor.
JPT
Participant
0 Kudos

Hello Horst

It seems like that CREATE OBJECT and NEW behave a little bit different when it comes to the parameter-transfer.

In the following Coding, we first had the "CREATE OBJECT" statement (#1) and it worked.
Then the coding got adapted to the new style with NEW (#3). During compiletime no issues here. But as soon as we executed the report again it caused a dump with "OBJECTS_OBJREF_NOT_ASSIGNED".
Example #4 also causes the same dump.
We know, that accessing dock_at_left should be done as in #2, but still were a bit surprised that it already worked in #1.
So behinde CREATE OBJECT the object gets created first and then parameters are passed, and in NEW it works a bit different..?

So 1:1 refactoring to NEW here could go wrong cause issues if you accessed variables like this.

Here is the coding:

  METHOD run.

    DATA:
      lo_docking  TYPE REF TO cl_gui_docking_container,
      lo_docking2 TYPE REF TO cl_gui_docking_container,
      lo_docking3 TYPE REF TO cl_gui_docking_container,
      lo_docking4 TYPE REF TO cl_gui_docking_container,
      lo_docking5 TYPE REF TO cl_gui_docking_container.

*   #1 works, despite lo_docking->dock_at_left does not exist?
    CREATE OBJECT lo_docking
      EXPORTING
        repid = sy-repid
        dynnr = '0300'
        side  = lo_docking->dock_at_left.

*   #2 "correct" usage of dock_at_left
    lo_docking2 = NEW #( repid = sy-repid
                        dynnr = '0300'
                        side  = cl_gui_docking_container=>dock_at_left ).

*   #3 same as #1, but here we cause a dump, bc of lo_docking3 is not initiated yet
    lo_docking3 = NEW #( repid = sy-repid
                         dynnr = '0300'
                         side  = lo_docking3->dock_at_left ).

*   #4 dumps as well, bc lo_docking5 is not created at all.
    CREATE OBJECT lo_docking4
      EXPORTING
        repid = sy-repid
        dynnr = '0300'
        side  = lo_docking5->dock_at_left.

  ENDMETHOD.