Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

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

TuncayKaraca
Active Contributor
0 Kudos

Hi,

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.

Tuncay

9 REPLIES 9

bjoern_weigand
Explorer
0 Kudos

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

Björn

0 Kudos

@Bjoern Weigand

Thanks for your response.

- If I use static class/method I can use directly like ZCL_CLASS=>method(), Right? So I don't see the point that why I need to use singleton design pattern.

- I don't need to create multiple instances of the class, since the methods code logic will be self-executable that don't need anything else. Just give parameters and return the results.

What about memory issue the book points out?

If I use a static method of the class, does SAP load just this method into the memory OR entire class (methods, attributes)?

Actually I don't want multiple instances and I don't want static methods because of this memory issue!

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? In this case the books says "Use instance methods" ...

0 Kudos

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.

Regards,

Naimesh Patel

naimesh_patel
Active Contributor
0 Kudos

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.

Regards,

Naimesh Patel

0 Kudos

@Naimesh Patel

So you recommend to use instance class/methods even though method logic is just self-executable. Right?

<h3>Example:</h3>

<h4>Instance option</h4>

CLASS zcl_class DEFINITION.

METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.

METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.

ENDCLASS

CLASS zcl_class IMPLEMENTATION.

METHOD add_1.

e_output = i_input + 1.

ENDMETHOD.

METHOD subtract_1.

e_output = i_input - 1.

ENDMETHOD.

ENDCLASS

CREATE OBJECT lo_object.

lo_object->add_1(

exporting i_input = 1

importing e_output = lv_output ).

lo_object->subtract_1(

exporting i_input = 2

importing e_output = lv_output2 ).

<h4>Static option</h4>

CLASS zcl_class DEFINITION.

CLASS-METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.

CLASS-METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.

ENDCLASS

CLASS zcl_class IMPLEMENTATION.

METHOD add_1.

e_output = i_input + 1.

ENDMETHOD.

METHOD subtract_1.

e_output = i_input - 1.

ENDMETHOD.

ENDCLASS

CREATE OBJECT lo_object.

lo_object->add_1(

zcl_class=>add_1(

exporting i_input = 1

importing e_output = lv_output ).

lo_object->subtract_1(

zcl_class=>subtract_1(

exporting i_input = 2

importing e_output = lv_output2 ).

So which option is best? Pros and Cons?

0 Kudos

In your example, I would say it doesn't matter, if you use the Static Class or the Instance class because you don't have any attribute.

Regards,

Naimesh Patel

0 Kudos

I think the discussion is rather academic. In ABAP public classes the default for all methods is instance and private. For whatever reason, I created a report once that uses just one class with static-only methods. I do not care about memory, we have some. I never saw memory problems resulting from code loaded - it's always the data.

In my class, whenever I do some maintenance and create an attribute or a method, the first error reported is that instance methods and attributes can only be used in instances, not in static methods.

Some time ago, I asked myself why I should use instance objects and methods when they are used just once. By now, I'd say it just does not hurt and has the advantages already named in this thread.

Regards,

Clemens

0 Kudos

I think that you make the things too complicated.

Naimesh has already mentioned the memory issues and like Clemens I would share the opinion that memory is rarely an issue.

Personally I use mostly instance methods, because OOP means the creation of instances and a class is normally like a template of objects and the objects are the heard and the soul of OOP.

If you have always only one instance of a class, I believe that you should rethink the usage of OOP.

In normal ABAP coding, static variables are also used rarely (like global variables) and it is the same in OOP. Static attributes are bound to the class. There a several cases where this can be used as an advantage (e.g. Singleton).

As I mentioned earlier: If you need only one instance, you can use static methods, but normally you should have multiple instances and then you should use instance methods and instance attributes.

But take into account that static attributes can only be used with static methods.

0 Kudos

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.