ABAP 740 – NEW Operator to instantiate the objects

By | August 25, 2014 | Concepts | 13,763 | 15

With ABAP release 740, you can use the NEW operator to instantiate the object. Read more to learn about NEW Operator.

Preface

In ABAP 740, we have NEW Operator. This operator can be used to instantiate the object, create a new data reference variables, new table, new workareas etc. In this article, I would try to show you how we can use NEW with the ABAP Objects. In future articles, we would cover the rest of the usage of NEW operator.

Using NEW operator, the object instantiation would be more cleaner. Something like:

NEW_Object_Creation_Variations

Example 1 – Passing Value to the Constructor

Without using the NEW operator, we instantiate the object using the CREATE OBJECT keyword. This would generate call the constructor of the class along with the parameters to the constructors. In this example, we have class LCL_REPORT which accept IV_REC as importing parameter for the CONSTRUCTOR. So, Typical object instantiation would be:

 
*
CLASS lcl_report DEFINITION.
  PUBLIC SECTION.
    METHODS:
      constructor IMPORTING iv_rec TYPE i,
      select_data,
      write_data.
  PRIVATE SECTION.
    DATA: v_rec TYPE i.
ENDCLASS.
*
START-OF-SELECTION.
  DATA: lo_report TYPE REF TO lcl_report.
  CREATE OBJECT lo_report
    EXPORTING iv_rec = 5.
  lo_report->select_data( ).
*
*
CLASS lcl_report IMPLEMENTATION.
  METHOD constructor.
    WRITE: / '------'.
    WRITE: / 'New Object', iv_rec.
    v_rec = iv_rec.
  ENDMETHOD.
  METHOD select_data.
    WRITE: / '   Selecting data', v_rec.
  ENDMETHOD.
  METHOD write_data.
    WRITE: / '   Writing data', v_rec.
  ENDMETHOD.
ENDCLASS.
 

Variation 1 – NEW with reference to Declared type

With NEW operator, you can simply instantiate or create the object like this. Here the data declaration is implicit. The variable O_REPORT would be declared with type of LCL_REPORT from the right side.

 
* Variation 1
  DATA(o_report) = NEW lcl_report( 1 ).
  o_report->select_data( ).
 

Variation 2 – Using Implicit reference type

Here we didn’t specify the class name after the NEW. When you use the # addition to the NEW, system would determine the type of the class of the variable on the left side. E.g. system would be able to determine LCL_REPORT from data declaration of O_REP.

 
* Variation 2
  DATA: o_rep TYPE REF TO lcl_report.
  o_rep = NEW #( 2 ).
  o_rep->select_data( ).
 

Variation 3 – Without reference variable

The object can be instantiated and respective methods can be executed without actually receiving the object reference back in any variable. In this case, the object would be created temporarily and would be available in the memory during the execution of the statement.

 
* Variation 3
  NEW lcl_report( 3 )->select_data( ).
 

Output

Using all variations into one, the output would be like:

NEW_Object_Creation_Output

Example 2 – Passing object reference to the CONSTRUCTOR

I’m sure, that you must got hang of NEW operator now. Lets take another example. In this case, the constructor would accept the object reference instead of the values. We can use the NEW operator as well for those parameters to assign the object.

Typical implementation would be:

 
*
CLASS lcl_sel DEFINITION.
  PUBLIC SECTION.
    DATA: v_rec TYPE i.
ENDCLASS.
*
CLASS lcl_report_new DEFINITION.
  PUBLIC SECTION.
    METHODS:
      constructor IMPORTING io_sel TYPE REF TO lcl_sel,
      select_data.
ENDCLASS.
*
*
START-OF-SELECTION.
  DATA: lo_sel_o TYPE REF TO lcl_sel.
  DATA: lo_rep_o TYPE REF TO lcl_report_new.
  CREATE OBJECT lo_sel_o.
  CREATE OBJECT lo_rep_o
    EXPORTING
      io_sel = lo_sel_o.
*
CLASS lcl_report_new IMPLEMENTATION.
  METHOD select_data.
  ENDMETHOD.
ENDCLASS.
 

Using NEW operator

Same code using the NEW operator:

 
  DATA(o_rep_new) = NEW lcl_report_new( NEW #( ) ).
  o_rep_new->select_data( ).
 
  DATA(o_rep_new1) = NEW lcl_report_new( NEW lcl_sel( ) ).
  o_rep_new1->select_data(  ).
 

As you can notice here, the object of LCL_SEL is created inline using the NEW. You can instantiate the object using the Implicit reference #( ) or using the explicit reference LCL_SEL( ).

Example 3 – Passing the value from Object attribute

You can also create the object and use the attributes of that object to pass into another object’s constructor parameter. In this example, the CONSTRUCTOR of LCL_REPORT_NEW_1 accepts the IV_NUM parameter. This would be set from the V_REC of object reference to LCL_SEL. The object reference to LCL_SEL is created inline using the NEW operator.

 
*
CLASS lcl_sel DEFINITION.
  PUBLIC SECTION.
    DATA: v_rec TYPE i.
    METHODS:
      constructor.
 
ENDCLASS.
*
CLASS lcl_report_new_1 DEFINITION.
  PUBLIC SECTION.
    METHODS:
      constructor IMPORTING iv_num TYPE i.
ENDCLASS.
*
START-OF-SELECTION.
  data: lo_rep_n_o type ref to lcl_report_new_1.
  data: lo_sel_n_o type ref to lcl_Sel.
  create object lo_sel_n_o.
  create object lo_rep_n_o
    exporting
      iv_num = lo_sel_n_o->v_rec.
*
CLASS lcl_report_new_1 IMPLEMENTATION.
  METHOD constructor.
    WRITE: / 'IV_NUM', iv_num.
  ENDMETHOD.
ENDCLASS.
*
CLASS lcl_sel IMPLEMENTATION.
  METHOD constructor.
    v_rec = 100.
  ENDMETHOD.
ENDCLASS.
 

Using NEW operator

With New operator, the entire logic can be wrapped in single line.

 
  DATA(o_rep_new_1) = NEW lcl_report_new_1( NEW lcl_sel( )->v_rec ).
 

Output of this code would be 100 as the 100 is set to the V_REC.

Method Chaining Possible?

This would raise the question on can we instantiate the object and at the same time call few methods to perform few activities before it would be passed as the argument to the constructor of the another class kind of method chaining. From ABAP help,

Method chainings cannot be specified between the constructor expression and the attribute here.

If you try to do so, compiler would produce the error:

NEW_Object_Error_method_chain

Would you use this or stay with CREATE OBJECT?

Table of Content – ABAP 740 Concepts

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments

15 Comments

  • WIllard

    moving to the NEW operator. Thank u

  • Uwe

    Good one.
    I’m using NEW.

  • steven oldner

    Using it today!!

  • WIllard

    I am trying the NEW operator and i have created an instance class ZCL_DATA and one instance public method called GET_DATA. When I try to instantiate it in an expression its giving an error message saying

    The method “NEW ZCL_DATA( … )->GET_DATA( )” does not
    have a RETURNING parameter. You cannot call it in expressions.

    Below is the expression that i am using
    DATA(lo_data) = NEW zcl_data( )->get_data( ).

  • Uwe

    @WIllard
    Maybe because “get_data” really doesn’t have a RETURNING parameter?

  • Hello Willard,

    In this case, you need to have a returning parameter in the get_data method. Because, you are trying to call the method as well from the chain, the left side variable would be the final result of the chain. so, the LO_DATA would be create and would hold the reference to the data, but not with the object reference to ZCL_DATA.

    You can call NEW ZCL_data( )->get_data( ) without receiving any result, means there is no evaluation of the result – No left or right side. As soon as you try to receive the result using =, the evaluation would take place between left side and final result from the right side.

    Thanks,
    Naimesh Patel

  • WIllard

    @Uwe
    I thought the NEW operator will perform a ‘miracle’ a miracle on this one. It doesnt have a returning parameter.

    In Naimesh’s example below

    DATA(o_rep_new_1) = NEW lcl_report_new_1( NEW lcl_sel( )->v_rec ).

    neither lcl_report_new_1 nor lcl_sel dont have returning parameters.

    Maybe I am missing something. May you please enlighten me

  • WIllard

    @Naimesh…

    Thank you

  • Hello Willard,

    DATA(o_rep_new_1) = NEW lcl_report_new_1( NEW lcl_sel( )->v_rec ).

    This would be first statement which would be executed. NEW lcl_sel( )->v_rec. LCL_SEL constructor would be called. V_REC would be set to 100. That 100 would passed as the IV_NUM to the constructor of LCL_REPORT_NEW_1.

    If I would have a statement like this:

     
    DATA(lo_data) = NEW lcl_report_new_1( NEW lcl_sel( )->v_rec )->select_data( ). 
     

    It would try to execute the SELECT_DATA and would send back the selected data to LO_DATA, provided SELECT_DATA is defined and implemented

     
    methods:
      select_data
        returning(ro_tab) type standard table.
    *
    method select_Data.
      select * from table xyz
        into table ro_tab up to 10 rows.
    endmethod.
     

    Thanks.

  • WIllard

    @Naimesh

    Brilliant!

    Thank you

  • Uwe

    Please don’t use RETURNING for tables or structures, because it’s always called by value. Use EXPORTING (called by reference) instead.

  • Himansu

    Like always, great. I have started to use it.

  • Aaron

    I need to get more used to ABAP Objects so this was a great tutorial not only on how people use local classes (instead of form…endform) and new elements in 7.4. Lastly I am amazed to see Uwe on here. I miss learning from Uwe at TechEd and SAP Partner functions and it doesn’t look like he’s speaking this year.

  • Uwe

    Hi Aaron,
    but I’m volonteering in a couple of hands-on sessions this year in Berlin: http://sessioncatalog.sapevents.com/go/agendabuilder.speakers/?l=85&speaker_id=9355&locale=en_US

  • Aaron

    Oh, nice. Now I just need to change my registration from Las Vegas to Berlin!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.