Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a local class?

0 Kudos

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!

1 ACCEPTED SOLUTION

maheshpalavalli
Active Contributor

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

6 REPLIES 6

maheshpalavalli
Active Contributor
0 Kudos

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.

0 Kudos

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.

matt
Active Contributor

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.

0 Kudos

Thank you for your information!

maheshpalavalli
Active Contributor

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

0 Kudos

Thank you very much!