Skip to Content

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

To report this post you need to login first.

27 Comments

You must be Logged on to comment or reply to a post.

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

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

    (0) 
    1. Horst Keller 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.
      (0) 
  3. 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

    (0) 
  4. 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 ….!!!!!!!!!  ๐Ÿ™‚ ๐Ÿ˜€

    (0) 
    1. Horst Keller 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.

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

        (0) 
        1. Horst Keller 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 ๐Ÿ˜‰

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

    (0) 
    1. Horst Keller 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

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

    (0) 
    1. Horst Keller 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

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

        (0) 
          1. 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(…)( ).



            (0) 
            1. Horst Keller 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

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

        (0) 

Leave a Reply