Skip to Content
Author's profile photo Horst Keller

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
                col2col1 = 1
                col2col2 = 2
                col3 = VALUE #( ( col1 = 1 col2 = 2 )
                                ( col1 = 3 col2 = 4 ) ) )
              ( col1 = 2
                col2col1 = 2
                col2col2 = 4
                col3 = VALUE #( ( col1 = 2 col2 = 4 )
                                ( col1 = 6 col2 = 8 ) ) ) ).

Assigned Tags

      30 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Custodio de Oliveira
      Custodio de Oliveira

      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

      Author's profile photo Manu Bhatnagar
      Manu Bhatnagar

      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

      Author's profile photo Jacques Nomssi
      Jacques Nomssi

      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author
      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.
      Author's profile photo Marc Augustin
      Marc Augustin

      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

      Author's profile photo Former Member
      Former Member

      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 ....!!!!!!!!!  🙂 😀

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      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.

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      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 😉

      Author's profile photo Former Member
      Former Member

      Thanks for providing the insight Horst 🙂

      Author's profile photo Hannes Ladstätter
      Hannes Ladstätter

      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      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

      Author's profile photo Christian Lechner
      Christian Lechner

      Hi,

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

      Cheers

      Christian

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

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

      Author's profile photo vamsilakshman pendurti
      vamsilakshman pendurti

      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      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

      Author's profile photo vamsilakshman pendurti
      vamsilakshman pendurti

      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 

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      NEW can also replace CREATE DATA.

      Horst

      Author's profile photo vamsilakshman pendurti
      vamsilakshman pendurti

      Thanks Horst ,

      Vamsi

      Author's profile photo Former Member
      Former Member

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



      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      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

      Author's profile photo Pavel Astashonok
      Pavel Astashonok

      How can we use dynamic types with NEW like we use them with CREATE DATA?

      CREATE DATA dref TYPE (type).

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      As documented, not.

       

      Author's profile photo Pavel Astashonok
      Pavel Astashonok

      And is it planned in future releases? It is quite logical step concerning NEW is a modern way of creating data elements.

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      I can't answer that question.

      Author's profile photo Siddharth Shah
      Siddharth Shah

      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      About the same difference as between CREATE DATA and DATA VALUE.

      Author's profile photo Paweł Karp
      Paweł Karp

      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ł

      Author's profile photo Harel Gilor
      Harel Gilor

      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 ?

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      No,

      There is no parameter AREA HANDLE for the NEW constructor.