Skip to Content

Global singleton class - public or protected/private instance generation?

Hello colleagues,

Recently, in abapdocu I read a guideline that describes a rule: "Declare the instance constructor in the public visibility section." with an exсeption: "The technical restrictions mentioned only apply to the processing of global classes.".

Okay, clear. But what about global singleton classes? Should we also use public section for constructors of such classes? I thought that protected or private instance generation should be defined for this pattern 😕.

Thanks,

Ilya

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    Oct 30, 2015 at 09:27 AM

    I thought that protected or private instance generation should be defined for this pattern

    Absolutely correct. If the instance creation is Public, then you can never impose the "singleton" behaviour!

    I think you are confusing "instantiation level" with "visibility".

    When defining a class as singleton i set the instantiation level to "Private/Protected" depending on the requirement & never to Public. You can still create the "Constructor" in the PUBLIC "visibility"

    As of ABAP 7.0 EhP2 you can define the CONSTRUCTOR in all visibility areas. (Ref. ABAP Keyword Documentation).

    BR,

    Suhas

    Add comment
    10|10000 characters needed characters exceeded

    • Suhas Saha wrote:

      Always declare the instance constructor of a global class in its public visibility section and independently of the instantiation specified by the CREATE addition in the class definition.

      That explains everything 😊. Seems like I understood it in a wrong way.

      Thanks a lot!