1

During the debugging procedure of a small code in Debug-mode, the class_contructor method is never executed. But it should be executed. I am wondering why!

Look at the lines between row 67 and 70!

REPORT ZSAPBC401_BAS_S3.

TYPE-POOLS icon.



CLASS lcl_airplane DEFINITION.

  PUBLIC SECTION.

    METHODS:

      CONSTRUCTOR
        IMPORTING
          iv_name TYPE string
          iv_planetype TYPE saplane-planetype
        EXCEPTIONS
          wrong_planetype,

      display_attributes.

    CLASS-METHODS:
      class_contructor,
      display_n_o_airplanes,
      get_n_o_airplanes
        RETURNING value(rv_count) TYPE i.


  PROTECTED SECTION.

    CONSTANTS:
         c_pos_1 TYPE i VALUE 30.


  PRIVATE SECTION.
    TYPES: ty_planetypes TYPE STANDARD TABLE OF SAPLANE WITH NON-UNIQUE KEY PLANETYPE.



    DATA:
          mv_name TYPE string,
          mv_planetype TYPE saplane-planetype,
          mv_weight TYPE saplane-weight,
          mv_tankcap TYPE saplane-tankcap.


    CLASS-DATA:
      gv_n_o_airplanes TYPE i,
      gt_planetypes TYPE ty_planetypes.

    CLASS-METHODS:
      get_technical_attributes
        IMPORTING
          iv_type TYPE saplane-planetype
        EXPORTING
          ev_weight TYPE saplane-weight
          ev_tankcap TYPE saplane-tankcap
        EXCEPTIONS
          wrong_planetype.

ENDCLASS. "lcl_airplane DEFINITION



CLASS lcl_airplane IMPLEMENTATION.

  METHOD CLASS_CONTRUCTOR.
    Write: 'Hello! I was here'.
    SELECT * FROM saplane INTO TABLE gt_planetypes.
  ENDMETHOD.

  METHOD constructor.
    mv_name = iv_name.
    mv_planetype = iv_planetype.
    get_technical_attributes(
                              EXPORTING
                                iv_type = iv_planetype
                              IMPORTING
                                ev_weight = mv_weight
                                ev_tankcap = mv_tankcap
                              EXCEPTIONS
                                wrong_planetype = 1 ).
    IF sy-subrc <> 0.
      RAISE wrong_planetype.
    ELSE.
      gv_n_o_airplanes = gv_n_o_airplanes + 1.
    ENDIF.

  ENDMETHOD.

  METHOD display_attributes.

    WRITE:
      / icon_ws_plane AS ICON,
      / 'Name of Airplane'(001) , AT c_pos_1 mv_name,
      / 'Type of Airplane:'(002), AT c_pos_1 mv_planetype,
      / 'Weight:'(003),           AT c_pos_1 mv_weight LEFT-JUSTIFIED,
      / 'Tank capacity:'(004),    AT c_pos_1 mv_tankcap LEFT-JUSTIFIED.
  ENDMETHOD.

  METHOD display_n_o_airplanes.
    SKIP.
    WRITE:
    / 'Number of airplanes:'(ca1), AT c_pos_1 gv_n_o_airplanes LEFT-JUSTIFIED.
  ENDMETHOD.

  METHOD get_n_o_airplanes.
    rv_count = gv_n_o_airplanes.
  ENDMETHOD. 

  METHOD get_technical_attributes.
    DATA: ls_planetype TYPE saplane.

*    SELECT * FROM saplane INTO TABLE gt_planetypes.
    READ TABLE gt_planetypes INTO ls_planetype WITH TABLE KEY planetype = iv_type TRANSPORTING weight tankcap.
    IF sy-subrc = 0.
       ev_weight = ls_planetype-weight.
       ev_tankcap = ls_planetype-tankcap.
       ELSE.
         RAISE wrong_planetype.
    ENDIF.
  ENDMETHOD.

ENDCLASS. "lcl_airplane IMPLEMENTATION


DATA:
      go_airplane TYPE REF TO lcl_airplane,
      gt_airplanes TYPE TABLE OF REF TO lcl_airplane,
      gv_count type i.



START-OF-SELECTION.
*******************

  lcl_airplane=>display_n_o_airplanes( ).


  CREATE OBJECT go_airplane
    EXPORTING
      iv_name         = 'LH Berlin'
      iv_planetype    = 'A321'
    EXCEPTIONS
      wrong_planetype = 1.

  IF sy-subrc = 0.
     APPEND go_airplane TO gt_airplanes.
  ENDIF.

  CREATE OBJECT go_airplane
    EXPORTING
      iv_name         = 'AA New York'
      iv_planetype    = '747-400'
    EXCEPTIONS
      wrong_planetype = 1.
  IF sy-subrc = 0.
    APPEND go_airplane TO gt_airplanes.
  ENDIF.

  CREATE OBJECT go_airplane
    EXPORTING
      iv_name         = 'US Hercules'
      iv_planetype    = '747-200F'
    EXCEPTIONS
      wrong_planetype = 1.
  IF sy-subrc = 0.
    APPEND go_airplane TO gt_airplanes.
  ENDIF.

  LOOP AT gt_airplanes INTO go_airplane.
    go_airplane->display_attributes( ).
  ENDLOOP.

  gv_count = lcl_airplane=>get_n_o_airplanes( ).
  SKIP 2.
  WRITE: / 'Number of airplanes'(ca1), gv_count.
Martin
  • 122
  • 9

3 Answers3

6

It does not work because you wrote class_contructor instead of class_constructor, at least this is how you wrote it in the TXT attached, I corrected the spelling error and it works fine.

Ovidiu Pocnet
  • 579
  • 12
  • 32
1

Your example program shows a lot of examples of bad programming styles - it looks like OOP at the first glance, but making the data and methods static turns this into a mess. That being said, you probably ran into a documented issue:

For this reason, static methods may be executed before the static constructor was ended.

vwegert
  • 18,371
  • 3
  • 37
  • 55
-1

That is because you never invoke it. It seems that you're confused; you have 2 methods whose names are very similar:

METHOD CONSTRUCTOR

and

CLASS-METHOD CLASS_CONTRUCTOR

In the event START-OF-SELECTION you invoke the constructor of LCL_AIRPLANE when you create an instance:

CREATE OBJECT go_airplane
  EXPORTING
    iv_name         = 'LH Berlin'
    iv_planetype    = 'A321'
  EXCEPTIONS
    wrong_planetype = 1.

IF sy-subrc = 0.
   APPEND go_airplane TO gt_airplanes.
ENDIF.

But the class method 'CLASS_CONTRUCTOR' is not invoked in your code and if you want to do it you must use:

lcl_airplane=>CLASS_CONTRUCTOR( ).
Martin
  • 122
  • 9
Nelson Miranda
  • 5,484
  • 5
  • 33
  • 54
  • 2
    This answer is wrong. A class constructor is called automatically by the system and should not ever be called manually. – vwegert Apr 14 '16 at 06:10
  • It is not about `class_constructor`. It is about a static method `class_contructor`. He made a typo, therefore it is not executed automatically at initialization time and though he has to invoke it manually. – Martin Apr 19 '16 at 12:42