With ABAP release 740, you can use the NEW operator to instantiate the object. Read more to learn about NEW Operator.
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:
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( ).
Using all variations into one, the output would be like:
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:
Would you use this or stay with CREATE OBJECT?
Table of Content – ABAP 740 Concepts
- ABAP 740 – NEW Operator to instantiate the objects
- ABAP 740 – NEW Operator to create ITAB entries
- ABAP 740 – VALUE Operator to create ITAB entries
- ABAP 740 – Table Expressions to Read & Modify ITAB line
- ABAP 740 – LINE_EXISTS to check record in ITAB
- ABAP 740 – Meshes – A new complex type of Structures
- ABAP 740 – Mesh Path – Forward and Inverse Association
- ABAP 740 – FOR Iteration Expression
- ABAP 740 SWITCH – Conditional Operator
- ABAP 740 – LOOP AT with GROUP BY
- ABAP 740 – Is CONSTANT not a Static Attribute anymore?
- SALV IDA (Integrated Data Access) – Introduction
- SALV IDA – Selection Conditions
- SALV IDA – New Calculated Fields
- SALV IDA – Column Settings
- SALV IDA – Add and Handle Hotspot (Hyperlink)