02-09-2019 4:27 PM
I read a blog and the demo in it uses a local class with "create private". I put this class definition and implement inside a report program, but I cannot create instance of this class. I cannot even call the class-method of this class in my program.
How can I use it? In addition, why do we want a class "create private"?
Thank you!
02-10-2019 4:32 AM
Hi Jerry,
Like Matthew said private classes are used for factory and singleton and you cannot create an instance for it outside the class.
The blog kind of split the class method into multiple snippets for explaining the code, but the OP didn't put all the code in one place 😄 😄 and he forgot to call the create_customer outside .. I think his intention was to call that method create_customer not to create the instance of the class outside.. you can also see except the create_customer(static) others are instance methods and the OP created the instance of the class inside that static method..(lo_driver)
So your create_customer method call is correct and you cannot create instance for that outside the class.
BR
Mahesh
02-09-2019 6:24 PM
You cannot create instance for a private class in your report directly.. about Ur method call, if you could provide your source code then it will be helpful and you should paste the link for that blog also so we can check if the issue is with the blog or Ur code..
BTW You can check help documentation which has more than information you solve your query.
02-09-2019 8:34 PM
This is the link of the blog. https://blogs.sap.com/2013/01/16/navigating-the-bopf-part-3-working-with-the-bopf-api/
I copy all the code in my program, then I can call a class-method at the end if I use START-OF-SELECTION. But I still cannot call an instance method.
This is my code:
REPORT z_bobf_demo.
" I copy the code from the blog blow
CLASS lcl_demo DEFINITION CREATE PRIVATE.
......
ENDCLASS.
CLASS lcl_demo IMPLEMENTATION.
...
ENDCLASS.
(Then, I try to call the method from this local class.)
START-OF-SELECTION.
lcl_demo=>create_customer( iv_customer_id = '01' ). " This line is working now.
DATA(lo_demo) = new lcl_demo( ). " This line still has error.
02-09-2019 10:11 PM
The blog looks a little confused to me. You could ask the author. But in answer to your question, create private is used to implement the factory and singleton patterns. If you don't know what they are, then try reading the articles about them in wikipedia, for example.
02-10-2019 6:31 AM
02-10-2019 4:32 AM
Hi Jerry,
Like Matthew said private classes are used for factory and singleton and you cannot create an instance for it outside the class.
The blog kind of split the class method into multiple snippets for explaining the code, but the OP didn't put all the code in one place 😄 😄 and he forgot to call the create_customer outside .. I think his intention was to call that method create_customer not to create the instance of the class outside.. you can also see except the create_customer(static) others are instance methods and the OP created the instance of the class inside that static method..(lo_driver)
So your create_customer method call is correct and you cannot create instance for that outside the class.
BR
Mahesh
02-10-2019 6:30 AM