Application Development and Automation 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: 
Read only

How to access local classes inside a global class?

js2
Product and Topic Expert
Product and Topic Expert
5,356

This may be a silly question to some.. but..

I have put a simple local class definition and implementation into the "Local Definitions/Implementations" section of my global class (SE24). However I am unable to reference this local class *anywhere* in my global class. According to what 'little' documentaiton I can find on this, the local class should be accessible within the global class. My local class is just called lcl_model. If I try to reference it with: "data mr_model type ref to lcl_model." it raises an error that "lcl_model is unknown" (tried in private, protected and public sections). I've also tried various friend attributes in the local definitions include to no avail.

Any ideas? Surely it's something silly...

Regards, Jason.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,924

You can access it in the method implementations and not in declaration of a method parameter( importing/exporting/changing). Please check if the local definitions is activated properly. It works for me.

8 REPLIES 8
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,925

You can access it in the method implementations and not in declaration of a method parameter( importing/exporting/changing). Please check if the local definitions is activated properly. It works for me.

Read only

js2
Product and Topic Expert
Product and Topic Expert
0 Likes
3,924

Thanks for the reply - you're correct - I can reference the class INSIDE a method but I cannot reference it in a class attribute nor in the parameter interface of any method.

I was trying to set my class up to allow dependacy injection for testing but its looking like thats just not possible with abap-oo in this case as I need to be able to pass in a reference to the local class to the method.

i.e. put my select statements into a local class which can be replaced with a test double in unit-tests.

Read only

0 Likes
3,924

Hi Jason,

it´s correct - local class definitions are not accesible from outside their surrounding class. I think this makes sense because otherwise the class would be global. Although, e. g. Java offers more possibilities here...

For your problem I would suggest to define a global interface which the parameter is typed to. Then you can have a local implementation (e. g. if the parameter is not filled) and the caller can produce it´s own implementation.

Btw.: Whe don´t  you implement your Unit Test as a local class of the test class?

Local testclasses and Interfaces for used objects is btw. our best practice.

Regards,

Stefan

Read only

js2
Product and Topic Expert
Product and Topic Expert
0 Likes
3,924

Thanks Stefan. The code I'm speaking of is not the actual test code. I'll use local test classes for that of course.

I think I'll switch to global classes and interfaces as you've suggested as local classes inside a global class are too limited. Thankyou.

Read only

0 Likes
3,924

OK. Plaease mark this thread as answered.

Read only

js2
Product and Topic Expert
Product and Topic Expert
0 Likes
3,924

The Jive iPhone app won't let me do anything except like and reply. Next time I look at this on the full website I'll close it out...

Read only

0 Likes
3,924

I've marked it as answered for you.

Read only

vonglan
Active Participant
0 Likes
3,280

You can of course use local classes for attributes in your global class, e.g.

 data mo_substitution type ref to lcl_substitution.

You have to define them in the separate tab "Class-relevant Local Types" in Eclipse. In SAPGUI, you can access it via menu only: Goto / Local Definitions / Class relevant local definitions