Static and Instance - ABAP OO
There is a lot of question ask in between instance and static .
I will try to explain my best to explain what and how .
So for static and instance both are use with two different word -
1- Constructor -
Instance constructor
Static constructor
2- Method -
Instance method
Static method
3- Attribute
Instance attribute
Static attribute
First we start with constructor .
Instance constructor - When we create any object and initialize , instance constructor has been used .
In other word initialization of any object we used instance constructor .
It is by default available in class implicitly or we will define explicitly .
An instance constructor can have importing and exception parameter .
Keywords - Constructor
Static Constructor - It is needs to define explicitly in class.
The static constructor cannot be called explicitly.
It is executed before when class is first access that is before any of action will be executed:
1) Creating an instance using CREATE_OBJECT
2) Addressing a static attribute using <classname>-><attrbute>
3) Calling a static attribute using CALL METHOD
4) Registering a static event handler
5) Registering an event handler method for a static event
Keywords - Class_Constructor
____________________________________________________________
As per concern of method -
Instance method - Instance methods are methods which can be only called using the object reference of class . Instance methods can access instance attributes and instance events.
Example -
DATA: lo_data TYPE REF TO lcl_data.
CLASS test DEFINITION.
PUBLIC SECTION.
METHODS:
get_data IMPORTING lv_date TYPE d.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD get_data.
ENDMETHOD.
ENDCLASS.
CREATE OBJECT lo_data.
lo_data->get_data( '19820731' ).
Static Method - Static methods are methods which can be called irrespective to the class instance. You can access only static attributes and static events within the Static method.
example -
DATA: lo_data TYPE REF TO lcl_data.
CLASS test DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
get_data IMPORTING lv_date TYPE d.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD get_data.
ENDMETHOD.
ENDCLASS.
test =>get_data( '19820731' ).
Static method cannot be redefine .
______________________________________________________
Attributes-
Attributes are internal data fields within a class that can have any ABAP data type. The state of an object is determined by the contents of its attributes. One kind of attribute is the reference variable. Reference variables allow you to create and address objects. Reference variables can be defined in classes, allowing you to access objects from within a class.
Instance Attributes
The contents of instance attributes define the instance-specific state of an object. You declare them using the DATA statement.
Static Attributes
The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA statement. They are accessible for the entire runtime of the class.
All of the objects in a class can access its static attributes. If you change a static attribute in an object, the change is visible in all other objects in the class.
I will try to explain my best to explain what and how .
So for static and instance both are use with two different word -
1- Constructor -
Instance constructor
Static constructor
2- Method -
Instance method
Static method
3- Attribute
Instance attribute
Static attribute
First we start with constructor .
Instance constructor - When we create any object and initialize , instance constructor has been used .
In other word initialization of any object we used instance constructor .
It is by default available in class implicitly or we will define explicitly .
An instance constructor can have importing and exception parameter .
Keywords - Constructor
Static Constructor - It is needs to define explicitly in class.
The static constructor cannot be called explicitly.
It is executed before when class is first access that is before any of action will be executed:
2) Addressing a static attribute using <classname>-><attrbute>
3) Calling a static attribute using CALL METHOD
4) Registering a static event handler
5) Registering an event handler method for a static event
Keywords - Class_Constructor
____________________________________________________________
As per concern of method -
Instance method - Instance methods are methods which can be only called using the object reference of class . Instance methods can access instance attributes and instance events.
Example -
DATA: lo_data TYPE REF TO lcl_data.
CLASS test DEFINITION.
PUBLIC SECTION.
METHODS:
get_data IMPORTING lv_date TYPE d.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD get_data.
ENDMETHOD.
ENDCLASS.
CREATE OBJECT lo_data.
lo_data->get_data( '19820731' ).
Static Method - Static methods are methods which can be called irrespective to the class instance. You can access only static attributes and static events within the Static method.
example -
DATA: lo_data TYPE REF TO lcl_data.
CLASS test DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
get_data IMPORTING lv_date TYPE d.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD get_data.
ENDMETHOD.
ENDCLASS.
test =>get_data( '19820731' ).
Static method cannot be redefine .
______________________________________________________
Attributes-
Attributes are internal data fields within a class that can have any ABAP data type. The state of an object is determined by the contents of its attributes. One kind of attribute is the reference variable. Reference variables allow you to create and address objects. Reference variables can be defined in classes, allowing you to access objects from within a class.
Instance Attributes
Static Attributes
All of the objects in a class can access its static attributes. If you change a static attribute in an object, the change is visible in all other objects in the class.
Comments
Post a Comment