Reputation: 60
In my code I want to call the CLASS_CONSTRUCTOR
method from the static method ADD_BOOK
.
However I receive this syntax error:
The direct call of the special method "CLASS_CONSTRUCTOR" is not possible.
Here is the code:
CLASS lcl_books DEFINITION.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
CLASS-METHODS add_book.
...
ENDCLASS.
CLASS lcl_books IMPLEMENTATION.
METHOD class_constructor.
SELECT * FROM zgib_bmabuecher INTO TABLE gt_return.
ENDMETHOD.
METHOD add_book.
DATA lf_check TYPE n VALUE 0.
LOOP AT gt_return INTO mf_books.
IF if_book-isbn = mf_books-isbn.
lf_check = 1.
ENDIF.
ENDLOOP.
IF lf_check = 0.
INSERT zgib_bmabuecher FROM if_book.
ENDIF.
lcl_books=>class_constructor( ).
ENDMETHOD.
ENDCLASS.
Upvotes: 1
Views: 1073
Reputation: 10524
You have all the information in the error message. You cannot (german: darfst nicht) call the class constructor explicitly. It is always called automatically and just once whenever the class is used for the first time.
If you want to reuse the coding of the class constructor then put it in another class method, for example like this.
CLASS lcl_books DEFINITION.
"...
PRIVATE SECTION.
CLASS-METHODS:
select_books.
"...
ENDCLASS.
CLASS lcl_books IMPLEMENTATION.
METHOD class_constructor.
select_books( ).
ENDMETHOD.
METHOD select_books.
SELECT * FROM zgib_bmabuecher INTO TABLE gt_return.
ENDMETHOD.
Then change your add_book
method to
METHOD add_book.
DATA lf_check TYPE n VALUE 0.
LOOP AT gt_return INTO mf_books.
IF if_book-isbn = mf_books-isbn.
lf_check = 1.
ENDIF.
ENDLOOP.
IF lf_check = 0.
INSERT zgib_bmabuecher FROM if_book.
ENDIF.
select_books( ).
ENDMETHOD.
ENDCLASS.
Upvotes: 8