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 in ooabap

Former Member
4,463

what is the functionality of the constructor.

what is the diff b/w static and instance constructor.

7 REPLIES 7
Read only

Former Member
3,554

Learn to search.

Read only

Former Member
0 Likes
3,554

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.

Read only

Former Member
0 Likes
3,554

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

Read only

Former Member
0 Likes
3,554

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

Read only

3,554

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 ).

Read only

prabhu_04
Explorer
0 Likes
3,554

It's a Special method it trigger whenever create a object for the class.

Instance Constructor:

  • Its specific to object
  • Whenever new object is created it get executed
  • In this method name should be CONSTRUCTOR.
  • It can contain only importing parameters and exceptions
  • Its executed only life time of each object.

Static constructor:

  • Its not specific to any object.
  • Its Executed in either of following two cases
  • when we access static components before creating any objects
  • when we create the first object of the class
  • In this method name should be CLASS_CONSTRUCTOR.
  • It can’t contain any parameters and exceptions.
  • its Executed only once in life time of entire class.
Read only

0 Likes
3,554

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