‎2008 Jun 11 11:57 AM
what is the functionality of the constructor.
what is the diff b/w static and instance constructor.
‎2008 Jun 11 11:58 AM
‎2008 Jun 11 12:01 PM
Constructor is the method which will be triggered when you create the object(system identifies and calls the method). Here you can pass import params only, and you can give exceptions also.
Static constructor is the method which will trigger before creating the Object. here you cannot pass any parameters to the static method. you can use only static variables inside it.
‎2008 Jun 11 12:02 PM
Hi
It's like the event initialization:
- the instance constructor: it's triggered as soon as an object (instance) is created;
- the static constructor: it's triggered only once;
Max
‎2008 Jun 11 12:44 PM
Hi Krishnaveni,
Static Constructor:
Static constructors are executed the first time you address a class. In case you address a static attribute declared in a superclass using the class name of a subclass, only the static constructor of the superclass is executed. There is only one static constructor.
Instance Constructor:
This is the first method which is called when an object is created. There can be a constructor for every instance of an object created.
Regards,
Sai
‎2020 Aug 26 2:08 PM
example :
CLASS test DEFINITION.
PUBLIC SECTION.
Data: num TYPE i.
CLASS-METHODS: class_constructor.
METHODS: constructor IMPORTING val type i.
endclass.
CLASS test IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE: /'INSIDE STATIC CONSTRUCTOR'.
ENDMETHOD.
METHOD CONSTRUCTOR.
num = val.
WRITE: / 'INSIDE INSTANCE CONSTRUCTOR val: ', num.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(obj) = NEW test( val = 10 ).
‎2020 Aug 26 2:13 PM
It's a Special method it trigger whenever create a object for the class.
Instance Constructor:
Static constructor:
‎2020 Sep 16 6:51 AM
Generally Constructor is a special method which is triggered whenever we create object for the class.
type 1 : instance constructor
> whenever we instantiate the object using the create object statement, instance constructor is triggered.
> Each class has an instance constructor by default.
> instance constructor have only importing parameter.
>instance constructor is executed n no. of time i.e n no. of objects created.
type 2 : static constructor
> also called class constructor.
> whenever the system access the class for the first time, static constructor is triggered.
> no importing parameters and no exceptions
> static constructor can be executed only once i.e when system access the class first time.
EXAMPLE:
CLASS ABC DEFINITION.
PUBLIC SECTION.
CLASS-METHODS : CLASS_CONSTRUCTOR. "STATIC CONSTRUCTOR
METHODS : CONSTRUCTOR. "INSTANCE CONSTRUCTOR
CLASS-DATA : LV TYPE I.
ENDCLASS.
CLASS ABC IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/ 'CLASS CONSTRUCTOR'.
ENDMETHOD.
METHOD CONSTRUCTOR.
WRITE:/ 'INSTANCE CONSTRUCTOR'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
ABC=>LV = 100.
DATA : OBJ TYPE REF TO ABC.
CREATE OBJECT OBJ.
Regards,
Varun Shukla