cancel
Showing results for 
Search instead for 
Did you mean: 

What is the need of the static method of class in real time

ksuman2000
Explorer
0 Kudos

I just want to make sure the use of static method of a class.

please share your ideas ,so it would helpful to all of  us to become clear on it.

Accepted Solutions (1)

Accepted Solutions (1)

ksuman2000
Explorer
0 Kudos

Hi All,

Finally i am giving the answer that's been compiled from your ideas.

1. Static method is used in various design patterns like Factory method, Singleton class etc.

2. Static method is used whenever we need to access same functionality for all the objects of the same class or all objects of sub classes and super class in inheritance hierarchy.

3. We can use static method of any global class in public section as like function module anywhere in system.

4. It never allows to redefine the functionality in across inheritance hierarchy.

5. Due to its buffer ability in the class, we can use this method efficiently for validations and select queries that are independent of any specific business functionality.

Answers (2)

Answers (2)

ramakrishnappa
Active Contributor
0 Kudos

Hi Suman,

Static method is also called as Class method because, it is used to access static attributes & global attributes without instantiating the class. The static attributes are loaded into memory when the class is called for first time.

Let us see the below example to understand the static method and static attributes


class name : ZCL_TEST

Private Static attribute : GV_APP_MODE  of type string

( It is used for storing current application modes like "DISPAY", "CHANGE" "CREATE" ).

Static method: SET_APP_MODE( VALUE importing parameter )

Static method: GET_APP_MODE( VALUE returning parameter )

Let us assume, that we have 3 web dynpro components WDA1, WDA2, WDA3 and here WDA1 is main component with create, change and display buttons and WDA2 & WDA3 are sub components used inside WDA1.

Scenario: the sub components have to behave as per application mode set by parent component.

when user presses a button "CREATE" in WDA1 component, we set the application mode as below

     ZCL_TEST=>SET_APP_MODE( VALUE = 'CREATE' ).

Now, the sub components WDA 2 & WDA3 should behave as per application mode, so we need to get the current application mode

inside WDA1 & WDA2....

            DATA lv_app_mode type string.

               lv_app_mode =  ZCL_TEST=>GET_APP_MODE( ).

          case lv_app_mode.

               when 'CREATE'.

                    " do some logic for create mode

               when 'CHANGE'.

                    " do some logic for change mode

               when 'DISPLAY'.

                    " do some logic for display mode

          endcase.

similarly all sub components can access the application mode set by parent component.

Hope this helps you.

Regards,

Rama

ksuman2000
Explorer
0 Kudos

Thank you for you information,but same we can achieve using static attributes in instance methods as well right.So what is specific use of static method?

Former Member
0 Kudos

static method works the same as function module (except for some restrictions, e.g. no screen programming and stricter syntax check). You benefit mostly of the "encapsulation" (really needed to be placed in the quotes), as you can have multiple methods grouped in one class. It is the same as with a function module inside a function group, but imagine e.g. a global constant. You can create one in a class and access it from anywhere, but global class in a program (or function group) is accessible only for that program in particular. You also can benefit of some kind of buffering with the static attributes (and the class-constructor). So in most cases, go ahead with static methods if you wanted to use function modules and use FM's only for the screen / dynpro part.

ksuman2000
Explorer
0 Kudos

Thank you for sharing your idea.I got it what you are trying to say.Yes we can call the static method of a global class wherever we want to call using class name and can use the functionality globally as like function module if it is in public section.Of course it may be one of the use.But what is specific use of it when we want to use in object orient world other than calling globally as like function module?

mahesh_jagnani
Participant
0 Kudos

Hello,¨

Case:-One class which has many instance method and many Instance attribute.

if you want to add one method to it which is for one functionality and which is no link with existing attribute.

In this case it is better to create static method.This is because if you create instance method and in the program create an object for that class,in this case memory will be assign to all those insatnce attribute also.

Mahesh

ksuman2000
Explorer
0 Kudos

Yes mahesh, static method memory wont be assigned to objects.Their memory will be maintained as separate static memory apart of instance memory.As you said, if we add the static method to existing class of all instance methods and instance attributes,we  should use only static attributes only in that static method to do operations because it can't access instance attributes right.All you said about the memory separation, But what is the specific use of using in applications.We no need to bother about the memory rather than functionality.

former_member184578
Active Contributor
0 Kudos

Hi,

static method is used to access/update static attributes( which can be accessed/same throughout all the instances of class) . And, you don't have to instantiate( create object for) the class to access static methods. They can be accessed using the class name itself.

Class_Name=>Method_Name( ). " static method call

Check this blog for more details: ABAP Static vs Instance method - Which to use when? - ABAP Help Blog

Hope this helps u,

Regards,

Kiran

ksuman2000
Explorer
0 Kudos

Thank you for your information.You gave idea of how to use technically about static method.But still waiting for the specific use of it in real time.