Skip to Content

Static Classes/Methods vs Objects/Instance Classes/Methods?


I am reading "Official ABAP Programming Guidelines" book. And I saw the rule:

Rule 5.3: Do Not Use Static Classes

Preferably use objects instead of static classes. If you don't want to have a multiple instantiation, you can use singletons.

I needed to create a global class and some methods under that. And there is no any object-oriented design idea exists. Instead of creating a function group/modules, I have decided to create a global class (even is a abstract class) and some static methods.So I directly use these static methods by using zcl_class=>method().

But the rule above says "Don't use static classes/methods, always use instance methods if even there is no object-oriented design".

The book listed several reasons, one for example

1-) Static classes are implicitly loaded first time they are used, and the corresponding static constructor -of available- is executed. They remain in the memory as long as the current internal session exists. Therefore, if you use static classes, you cannot actually control the time of initialization and have no option to release the memory.

So if I use a static class/method in a subroutine, it will be loaded into memory and it will stay in the memory till I close the program.

But if I use instance class/method, I can CREATE OBJECT lo_object TYPE REF TO zcl_class then use method lo_object->method(), then I can FREE lo_object to delete from the memory. Is my understanding correct?

Any idea? What do you prefer Static Class OR Object/Instance Class?

Thanks in advance.


Add comment
10|10000 characters needed characters exceeded

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Sep 16, 2010 at 03:32 PM

    Hi Tuncay,

    I guess, it depends on the usage of the class.

    If you try to use a singleton design pattern, it is, in my opinion, impossible to use instance methods, because you need a static attribute to hold the object of the class and static attributes can only be used by static methods.

    Therefore I would summarize:

    - Use static methods, if you want to use the singleton design pattern (have exactly one instance at runtime)

    - Use instance methods, if you can have more than one instance at runtime

    I hope that answers the question.

    Best Regards


    Add comment
    10|10000 characters needed characters exceeded

    • Your question is to Bjoern, but I will try to answer..!

      Let's say we have a Class and all methods are instance methods but each method has own executable code without depending on the instance. So it is obvious the methods are logically static methods?

      The memory which we all refer here is about the Attributes, not the Methods. Methods don't have any memory assigned at time of loading. The memory would be assigned to the LOCAL data declaration within the method when that particular method is called at runtime. This memory binding would be cleared by the memory management (Garbage Collector) at end of the method call. This true for both Static and Instance.

      But the attributes consume the memory. if you create Static attributes, system would assign the memory while program loading. If you have instance attributes, the memory would be only assigned when you instantiate the object.

      I would suggest you to create the Instance attributes, instantiate the object and use that in your program.


      Naimesh Patel

  • Posted on Sep 16, 2010 at 03:36 PM

    As noted in the book, All Static Variables are loaded in the memory on the LOAD-OF-PROGRAM the same way all the Global variables of any Function Group. This will remain in the memory until you explicitly Free them.

    On the other side, unless you use the CREATE OBJECT statement, the memory will not be assigned to the instance variables. They will be loaded in the memory when Object is instantiated. ABAP Garbage collector will remove any un-referenced Objects from the memory.

    So, as per your usage, the memory would be allocated when you're subroutine gets executed, if you are using the Instance object.


    Naimesh Patel

    Add comment
    10|10000 characters needed characters exceeded

    • I have decided to use objects/instance instead of static classes as the book says. So even though the logic and parameters of the method are static, I have created the method as instance. I think this methodology has more advantages.

      It was a good discussion. Thank you all.