ABAP Objects Tips

Long before (or maybe not so long before), ABAP was written with subroutines and function calls. But in recent times, due to various reasons, developers are forced to use the Object Oriented Programming. Hands down, Object Oriented programming is the better way to write programs but I have a problem with the forced approach. This has led to a time, where many developers are writing ABAP objects without even understanding them. Many developers really find it easy but initially I found OOPs pretty intimidating as I have never written (from scratch) code in JAVA or C++ during my college days. My friends, who are like me, fear not. We all can learn and more importantly understand and master ABAP Objects. As usual, several sources are available on the internet, so I am just concentrating on the tips:

The difference between STATIC and INSTANCE methods:

STATIC method looks easier to call but it comes with a price. It cannot be REDEFINED. This should be enough to stop you from creating STATIC methods. But what if, you are creating local classes and you know that you do not need redefinition. STATIC Method has 1 more problem. It is static and uses only static variables. So the values of the variables will remain same throughout the program. In order to feel the difference, declare a static variable in a subroutine and call the subroutine multiple times in your program. You will see that all the DATA variables have instantiated but the STATIC variable remembers the old value. This makes STATIC variable tricky and you need to be extra careful with them. Let me draw the parallel with Internet Browsing. Open Chrome in normal mode and no matter how many new windows you open, it will remember the content of the other windows. Now, open Chrome in Incognito mode. Every time you open a new window, you will get the same refreshed state. So the first case is STATIC Method call and the incognito mode is the Instance Method call. It is like keeping the history of your internet browsing. It is great and helpful but you need to be careful. Just avoid it, and write few extra lines of code.

Use STATIC Method only when it is necessary and it is necessary in generally 2 cases. You need to use the method before creating in the implementation and objects or you need to keep the values of the variables same throughout the call of the program.

Constructors:

You have read the definition that it is called with CREATE Object Statement. But it is a special method. It can be executed only once for each instance and obviously, it is getting triggered before any other instance method. So, if there is something which you want to ensure before any of your instantiated methods triggers, you need to put that in the CONSTRUCTOR. Best Analogy, I can find is the Matrix movie. When Morpheus was explaining Neo, about his first training program. Before loading the actual program, the Construct is loaded. Because Construct has provided the base for the other program to load. Or in SAP terms, can default the values. It can also be used if you need to ensure it is triggered only (automatically) once for an instance. Maybe Matrix was designed following OOPs approach.

Rating: 0 / 5 (0 votes)