Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Constructor Vs. Method Qustion

Former Member
0 Likes
1,114

Hi,

i learn now about constructor in Abap and my question is ,

what is the advantage and disadvantage of using constructor oo,that i can build method that do the same action

e.g.

Regular Method:

    METHODS: set_attributes IMPORTING
             im_name TYPE string
             im_planetype TYPE saplane-planetype,
             display_attributes.

Constructor :


    METHODS: constructor IMPORTING
    im_name TYPE string
    im_planetype TYPE saplane-planetype,
    display_attributes.

in the constructor i use:

  CREATE OBJECT r_plane
    EXPORTING
      im_name      = 'LH_Barlin'
      im_planetype = 'a321'.

and in Method :

 CREATE OBJECT r_plane.
  r_plane->set_attributes( im_name = 'LH_usa' im_planetype = 'bbb' ).

and both commend do the same :

Regards

1 ACCEPTED SOLUTION
Read only

rainer_hbenthal
Active Contributor
0 Likes
965

The constructor is nothiing else but a method which is called automatically when the class is instantiated.

6 REPLIES 6
Read only

rainer_hbenthal
Active Contributor
0 Likes
966

The constructor is nothiing else but a method which is called automatically when the class is instantiated.

Read only

0 Likes
965

Hi Rainer Hübenthal ,

Thanks ,

if i can do it in method so way to have it (constructor) ?

Regards

Read only

0 Likes
965

First, you should post these questions in the ABAP Objects forum.

Anyway, you use the constructor to force certain actions when the instance is created. If you as designer of the class require that two attributes be set, then making them obligatory parameters ensures that an object can't be instantiated without those parameters being given some values. You could include some validation in the constructor as well to make sure that the values are valid.

If you don't put them in a constructor, you are relying on future developers - possibly even yourself - remembering to set those parameters after they've created.

One other advantage - it's one line of ABAP instead of three!

As ever though, which is better comes down to what specifically you're trying to do. It is usually better programming practice to leave as little to luck and remembering, as possible.

matt

Read only

Former Member
0 Likes
965
Read only

Former Member
0 Likes
965

Hi,

In constructor the method will execute automatically when the class has been instanced.

But for the normal method we have to call the method explicitly.

In some cases we need constructor so that some piece of code is executed automatically when the class is initiated.

Regards

Manjari.

Read only

narin_nandivada3
Active Contributor
0 Likes
965

Hi Cosmo Kramer,

Please check these threads..

/message/5904557#5904557 [original link is broken]

Please go through the below SAP HELP

[Constructor Vs Method |http://help.sap.com/erp2005_ehp_03/helpdata/EN/08/d27c03b81011d194f60000e8353423/content.htm]

Hope this would help you to understand the difference..

Good luck

Narin