Static Method VS Instance Method

 Introduction:

  • We will cover the following basic concepts: 
  1. Creating a Global Class in SE24.
  2. Understand the basics of Methods.
  3. Understand the basics of 'Types of Methods'.
  4. Define and Implement a Static Method & an Instance Method.
  5. Call a Static and an Instance Method in the SE38 program.
  6. Bonus Point.

Lets Start:
  1. Create a Global Class: ZGLOBAL_CLASS in Se24.


  • Choose the following options to create a Class.
    • Choose 'Class' Radio Button.

 
  •  Choose 'Usual ABAP Class' and 'Final' Check Box.


      2. Now let us understand the basics of Methods.
  • Methods are just like Function Modules. 
  • Check out the below comparison that will help you to get a basic understanding of Methods.



        3. Now let us understand the basics of  'Types of Methods' with a below simple comparison.




    
       4. Now let us define and implement a Static Method & an Instance Method.
  • Create METHOD_INSTANCE & METHOD_STATIC in Methods Tab.
  • And set the level of Methods as Instance & Static respectively as shown below.


 
  • To see how the generated code looks in the Public Section follow the below steps.
    • Goto->Sections->Public section.


    • We can see generated code as below.


  • Create an implementation of the METHOD_INSTANCE as shown below.


  • Similarly create an implementation for METHOD_STATIC as shown below.



     
      5. Now let us call these Static and the Instance Methods in the SE38 program.

  • First let us call the Static Method METHOD_STATIC.
    • We call METHOD_STATIC directly using the Class Component selector ( => ) symbol.
    • There is no need to create an Object for the Static Method.


    • Now Save, Check & Activate the Program. the program activates without any error.
    • Execute the program, The method METHOD_STATIC gets called successfully and the WRITE statement inside the implementation of the method gets executed.


  • Now let us code for the call of Instance Method METHOD_INSTANCE.
    • Here we call the Instance Method just like we called our Static Method using Class Component Selector ( => ) symbol.


    • Now Save, Check &  try to Activate the Program, we will an ERROR as shown below.


    • As the error suggests we cannot call an Instance Method using Class=>Method.
    • So we will create an Object with the class reference parameter LR_CLASS.
    • Call the METHOD_INSTANCE using the Created Object as shown below.


    • Now Save, Check & Activate the Program. the program activates without any error.
    • Execute the program, The methods METHOD_STATIC & METHOD_INSTANCE get called successfully and the WRITE statement inside the implementation of the methods get executed.


    • Now, we know the basics of Theoretical and Practical implementation of  STATIC and INSTANCE Methods.

     6. Now for the BONUS POINT.
  • Let us Try to Call the Static Method METHOD_STATIC using our created Object LR_CLASS. That is, call the Static Method using 'Object Component Selector' just like the Instance Method.

  • Guess what? Surprisingly it works.


  • Find the complete sample code below.
REPORT zglobal_class_methods1.
*Consume the methods of Class ZGLOBAL_CLASS

*Call the Static Method of the Class
zglobal_class=>method_static( ).

*Call the Instance Method of the Class
*Declare local variable concerning the class
DATA:lr_class TYPE REF TO zglobal_class.

*Create an object for the class
CREATE OBJECT lr_class.
*Cursor to Next Line
WRITE /.
*Call Method
CALL METHOD lr_class->method_instance.

*Call the Static Method of the Class using Created Object
*Cursor to Next Line
WRITE /.
*Cursor to Next Line
WRITE / 'Using Class OBJECT:'.
*Call Method
CALL METHOD lr_class->method_static.

Comments

Popular posts from this blog

Class Visibility

Special Method: Constructor

Class Inheritance