Object Oriented Programming in ABAP - Understanding the concept of static and instance
The class components that have been declared fall under two category static and instance. Now what do we mean by this actually? As we know class is only a logical modelling of an
object .It does not occupies any memory space during program execution only the instance of an class (objects) occupies memory space. We know a class can have multiple instance i.e
multiple object can refer to same class. And in oop separate memory are allocated for all different objects and these memory area are not shareable by other objects . But it is
possible to have only one common memeory area for all instance of a class. The class components that share a common memory area for all the class instance are
static components. The class components that have separate memory area for separate instance are instance components.
A point of note that a class can have static as well as instance components. Only static methods can access static attribute and static event can be raised by static methods and the same is true for instance methods attributes and events.
Static and Instance Constructor
As we know c onstructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the starting state of a new object or class.
There are two types of constructors - instance constructors and staticconstructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.
The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:
IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..
and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been
created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS
additions in the CREATE OBJECT statement.The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR.
You declare it in the public section as follows:CLASS-METHODS CLASS_CONSTRUCTOR. and implement it in the implementation section like any other method. The static constructor
has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the
components of its own class.