Application Development 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: 

constructor

Former Member
0 Kudos

i have created a class which has a constructor which imports the value of a and b.

for this particular class iam creating an object obj1 by exporting the values for a and b to the constructor.

then iam creating an object called obj1 and obj2 without passing any parameters. because these objects should not execute that constructor rather it should call all the other methods.

for this program it shows an error message saying " the obligatory parameter A has no value assigned to it" at the lace where iam using create object obj2 and obj3.

can anyone help me what has to be done to resolve this.

thanks,

Phyrose.

4 REPLIES 4

marcelo_ramos
Active Contributor
0 Kudos

Hi Camila,

Try to define the parameters as optional as follow.


    METHODS constructor 
              IMPORTING field1 TYPE char01 OPTIONAL
                        field2 TYPE char10 OPTIONAL.

During the Processing of Constructor if you need you can use the statement 'IS SUPPLIED' to check is the parameters was used.


  METHOD constructor.

    IF field1 IS SUPPLIED.
      ...
    ENDIF.

    ...

  ENDMETHOD.

Regards.

Marcelo Ramos

sreemsft
Contributor
0 Kudos

Hi Phyrose,

When you define an instance constructor in your class, it would be called when ever you create an instance of a particular class.

Lets say you are going to create 3 instances of a class, your instance constructor would execute for 3 times.

You cannot restrict that with out triggering, once you declare an Instance Constructor in your class.

<i>Once you pass some importing parameters in a Constructor method, You should pass the parameters, while you create the object. If you do not pass any parameters it would throw an error.</i>

Check the below code for the example of a Constructor.


REPORT  Y_TEMP_OOABAP.

*----------------------------------------------------------------------*
*       CLASS C1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C1 DEFINITION.
  PUBLIC SECTION.
    METHODS: CONSTRUCTOR IMPORTING NUM1 TYPE I.
ENDCLASS.                    "C1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS C1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS C1 IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    WRITE:/ NUM1.
  ENDMETHOD.                    "CONSTRUCTOR
ENDCLASS.                    "C1 IMPLEMENTATION

START-OF-SELECTION.

DATA: OREF TYPE REF TO C1.

<b>CREATE OBJECT OREF EXPORTING NUM1 = 5.</b>

In the above example an instance constructor will trigger for one time, when it comes to the create object statement.

As i have importing parameters to my constructor method, i must pass the exporting parameters while I create the object reference.

Hope this clears your doubt.

Thanks,

Sreekanth

<i>Reward each helpful answer.</i>

uwe_schieferstein
Active Contributor
0 Kudos

Hello Phyrose

The <b>implicit </b>CONSTRUCTOR (of root object <b>OBJECT</b>) is only called if no CONSTRUCTOR method is defined for your class.

Regards

Uwe

marcelo_ramos
Active Contributor
0 Kudos

Hi Camila,

I Noted that you have a lot of unclosed thread, so I think you should read all <a href="https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/rulesofEngagement&">POST'S RULES</a> after you must close this and all yours thread that the question was answered !

Thanks !

Best Regards.

Marcelo Ramos