Skip to Content
avatar image
Former Member

Use of Local Classes

Hi OO Gurus,

I have this nagging question about the local classes in ABAP if we recommend the use of them any more.

I have come through some discussions where it has been said that they are not recommended any more.

Can you please throw some light on use of local classes. Also when the local classes should be used and when global classes? Most of the times, I have seen people using global classes only.

Thanks in Advance,

Raveesh

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    Jun 29, 2011 at 06:58 AM

    So far I had only once a chance to use local clasess in real case. It was for a BSP project we were working on. I had controller class which received any user (http) request. As this controller was dedicated to do other stuff too, I was not very keen on encapsulating all request handling within private method's of this controller. Creating separate global classes would also mean they would be exposed to the world, which I wanted to avoid (due to tight coupling to project). So I decided to delegate this task to local class object. This allowed me to separate responsibility b/w controller and local controller's class. Each had its own unique task to do.

    From OO design it has also one more advantage. I could easily implement several local classes and via composition use appropriate local class object based on view I used. So I could have several views handled by different local class objects which would share some common interface (but implement it in different manner). This interface would be all my client (controller) knows about local objects handling requests. So by means of strategy pattern I could dynamically decide which handler I want to use for which view.

    To conclude. I believe I can live w/o local classes, but certainly there should be some balance b/w exposing parts of the application to world and inner complexity hidden behind either private methods or local classes.

    Regards

    Marcin

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      You forgot a few remarks above: from 7.02, as Suhas said, SAP recommends to use them (or global classes) instead of forms/function modules, and they are better suited for use of control framework (I don't see why we should create a global class, but this is explained in your point 2). A few things can't be achieved using local classes as shared objects...

      raveesh saurabh wrote

      > 1. Local Classes can be used for modularization purposes, or for encapsulating some independent functioanlity within a class as local class.

      This point is not needed: it is not specific to local classes, it applies to global classes too. I think point 2 explains it.

      Sandra

      PS: I agree with Marcin 😊

  • avatar image
    Former Member
    Jun 28, 2011 at 05:43 PM

    Also if possible, the reasons behind not using them.

    Thanks,

    Raveesh

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Abhinava,

      1. Local Classes are not 100% object oriented. They depend on some Program for their existence.

      My thoughts, local classes are as object oriented as their counter parts(global class). A global class too depends on some program for its use, the use of a class(in a program or a class) doesn't decide if it is object oriented or not. A class in SAP ABAP (any other language) irrespective of local/global has to adhere to the OOP principles, and if it does it is Object Oriented.

      Regards,

      Chen

  • Jun 29, 2011 at 05:28 AM

    Hi Friend,

    another important usage of local class is that it can be used as generation of ABAP unit test class. As a approach of test-driven development in ABAP, you can use the build in functionality in SE80 to generate local test class of your productive class, and then write unit testing code in those generated lobal test class. You can search "ABAP Unit" as key word in abap document.

    Best Regards,

    Jerry

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 11, 2011 at 07:00 AM

    Hi All,

    I am closing the thread. Only point left is whether it is wise to use the global classes for modularizing your program.I think it is not wise to use global classes just for the sake of modularization of the program. I hope that some expert throws some light into it.

    Thanks & Warm Regards,

    Raveesh

    Add comment
    10|10000 characters needed characters exceeded

    • I'm also bit late in replying but will give my 2 cents.

      I would suggest not to create Business Local Classes in the global class or in the program if possible. While designing the local class, we may not realize that it may be used in future. More over if you have local class declared in the global class, they are not available in the inherited classes of the global class. So, ultimately, you would think why did you create that local class, which you need in the inherited class.

      Local classes in the program are fine if they are event handlers for some ALVs as they wont be "alive" outside the application. But if you have a global class which act as a wrapper to generate the ALV, you should create a private event handler method in the same class.

      Along with other uses, Local classes are also used in enhancing the global classes.

      CLASS LCL_ZTEST_NP_123 DEFINITION.
      PUBLIC SECTION.
      CLASS-DATA OBJ TYPE REF TO LCL_ZTEST_NP_123.
      DATA CORE_OBJECT TYPE REF TO ZCL_TEST_NP .
       INTERFACES IPR_ZTEST_NP_123.
        METHODS:
         CONSTRUCTOR IMPORTING CORE_OBJECT
           TYPE REF TO ZCL_TEST_NP OPTIONAL.
      ENDCLASS.
      CLASS LCL_ZTEST_NP_123 IMPLEMENTATION.
      METHOD CONSTRUCTOR.
        ME->CORE_OBJECT = CORE_OBJECT.
      ENDMETHOD.
      
      METHOD IPR_ZTEST_NP_123~M_1.
      *"------------------------------------------------------------------------*
      *" Declaration of PRE-method, do not insert any comments here please!
      *"
      *"methods M_1 .
      *"------------------------------------------------------------------------*
      
      ENDMETHOD.
      ENDCLASS.
      

      Regards,

      Naimesh Patel

  • Jul 01, 2011 at 08:10 PM

    My practice is to use local classes only in small, report-like applications. And occasionally in global classes, when I need something small as an object. For everything else, especially when it's modelling an object that could be reused, then I use global classes.

    For large applications, say using the MVC pattern, I'll use global classes simply because they're easier to handle than local ones.

    The trick is, as far as possible, to make your classes possible to reuse. As soon as you use local classes, you lose that opportunity. In the years I've been developing OO, I've reused classes maybe four years after I first wrote them, which is always cool.

    Edited by: Matt on Jul 1, 2011 10:10 PM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 04, 2011 at 02:49 PM

    Hi Marcin, Sandra

    SDN is a place where best people of the trade helps you selflessly even though they don't know you. I really am thankful for it.

    @ Sandra: Regarding your point that even global classes can be used for modularization: Technically it is possible to do so, but is it logical to create one more repository object just for the sake of modularizing your program or class? I have a feeling that it is better to modularize using local classes rather than global classes since we are creating a repository object just to arrange our code, and that's why I had added this point to use local classes for modularization.

    Please let me know if I am missing something, or my understanding is flawed.

    Thanks once again for your help.

    Warm Regards,

    Raveesh

    Add comment
    10|10000 characters needed characters exceeded