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: 

Singleton in ABAP

0 Kudos

Hi,

1) I have created a class with its instantiation set to private.

2) Declared a static attribute 'instance' in the class with TYPE REF to same class.

3) In the class constructor checked if instance is initial if it is then created an object.

if instance initial.

creat'e object instance.

endif.

4) Created a method get_instance which exports the 'instance' attribute of the class.

I suppose this is fine for implementing the Singleton class.

Created a sample program where I call get_instance of this class.

Open two sessions of SE80 and execute this program in debug mode. I see that each program is executing the class constructor and instance is initial every time.

If instance is initial in each execution (with two parllel executions) then what is the purpose of declaring it as static.

Since its initial create object is executed every time in

each execution. Then the instance is not unique and its not singleton.

Can somebody explain signleton in ABAP. Why is it different from JAVA.

Regards,

Sesh

1 ACCEPTED SOLUTION

0 Kudos

Hi Sesh,

Your class object exists per default once per session. As of release 6.40 you can create objects in the shared memory (with addition AREA HANDLE), thus session-independent! For that, you need to mark the class as shared-memory enabled. This should create a singleton as in Java.

Best regards,

Georg

2 REPLIES 2

0 Kudos

Hi Sesh,

Your class object exists per default once per session. As of release 6.40 you can create objects in the shared memory (with addition AREA HANDLE), thus session-independent! For that, you need to mark the class as shared-memory enabled. This should create a singleton as in Java.

Best regards,

Georg

0 Kudos

Hi Georg,

I am aware of the Shared Objects concept. What I was not aware was Singleton implementation model of java do not work in ABAP. Any way thanks for the reply. I am awarding you some points for the reply :-).

Regards,

Sesh