Table of Contents
Before discussing the differences between a Singleton method and Static method, let’ have a quick introduction. A method is a block of codes that defines behaviour of an object of the class. The method can access all the attributes of the class and can change the object data content.
Static and Singleton methods in SAP ABAP are very different in their usage and implementation. So it is very important to choose wisely either of them in our WRICEF.
Sometimes there is a need to instantiate an object at a time, that means only one instance is required at one point of time. This is a very common requirement while designing an application. To achieve such a scenario, there is one concept of ‘Singleton Pattern’ or ‘Singleton Class’ in OOABAP.
A class is said to be a singleton class if it can have the utmost one instance only.
Static methods are defined using the CLASS-METHODS statement. They are called only once, when the program is started, irrespective to the class instance, and don’t get reinitialized throughout the execution of the program. They can access only static attributes and can trigger only static events of the class. They can be directly accessed by the class name itself.
When shall we use singleton method and when to go for static methods?
Static methods are the one that are meant to store the initialization information and should be available globally. Whereas the most wide usage of singleton method is creating logs and single sign-on.
Difference between Singleton Method and Static Method
|Singleton Method||Static Method|
|It is a design pattern.||It is not a design pattern.|
|It can be instantiated only once.||It is triggered by a static component (static class, attributes and events) only and no instance components.|
|It is implemented in a specific scenario where we cannot have multiple instances like login.||It is widely used for single time initialization.|