Skip to Content

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

27 Comments
You must be Logged on to comment or reply to a post.
  • 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

  • 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

      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.
  • 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,

      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.

      • 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 ๐Ÿ˜‰

  • 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

  • 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