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

Local class # Object creation from another program

arunneerolil
Contributor
0 Likes
4,260

Is there any way to create an object of a local class in program A in program B ?

REPORT Z_REPORT_A.
CLASS LCL_LOCAL_CLASS DEFINTION.
PUBLIC SECTION.
METHODS: say_foo.
ENDCLASS.
CLASS LCL_LOCAL_CLASS IMPLEMENTATION.
METHOD say_foo.
WRITE 'Foo from program Z_REPORT_A'.
ENDMETHOD.
ENDCLASS.

REPORT Z_REPORT_B.

Can I create an object of

LCL_LOCAL_CLASS

in

Z_REPORT_B

??
1 ACCEPTED SOLUTION
Read only

nomssi
Active Contributor
3,514

Horst once said: "You shouldn't do that but you can by using absolute type names."

15 REPLIES 15
Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
3,514

Not recommended! (see comments): You can put local class definition and implementation into the INCLUDE and use this include in both programs.

But it sounds it would be better to create global class? Reusability 🙂

-- Tomas --
Read only

matt
Active Contributor
3,514

Includes for re-use are indeed very very bad.

One project I was on decided to use an include in hundreds of BW routines. It all worked nicely until a change needed to be made... and 100s of BW flows had to be regenerated in production. Took quite a while.

The reason an include was used was the the outsourcing partner's staff weren't technically competent to do much beyond inserting include statements.

Read only

3,514

I remember a hotline issue with macros in mutliply used includes, arrgh ...

Read only

0 Likes
3,514

Edited, thank you.

It is true that I do not like debug through a lot of standard multiple used includes...

-- Tomas --
Read only

3,514

I believe this might come as a surprise to many long time ABAPers. Judging by the standard SAP code, at some point using the same INCLUDE in multiple places was The Thing. Personally, I haven't even heard that officially changed. I just happened to dislike INCLUDEs personally (especially those standard ones that tend to generate an annoying "what's the main program for this?" pop-up, ugh) and avoid them like plague. But I wouldn't have known it is a no-no these days to reuse them. Kind of thought that was their whole point... Huh. Well, that's my "learned something today". 🙂

Read only

0 Likes
3,514

Hmm, maybe if you read the ABAP Programming Guidelines (that were also published as a book about ten years ago) from beginning to end you'll find one or the other additional rule that you didn't know of 😉

Read only

matt
Active Contributor
3,514

At one time it was considered a nice way of organising code. There were even common memory areas - take the horror of global variables to a new level!

Back in pre-2000, there was a lot of bad practice programming. I guess we just didn't know it was bad practice - or maybe it was only known inside the ivory towers of academia. Now we know what's good and bad*. One of the beneficial side effects of the internet... 🙂

* For people who can be bothered to look. Apparently, some people still use prefixes to indicate the types of their variables. 😉

Read only

pokrakam
Active Contributor
3,514

No you can't, the clue is in the word "Local". Use a global class.

It is however possible to use an instance of a local class elsewhere.

Also, do not use includes in multiple programs. https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenmultiple_use_include_g...

Read only

nomssi
Active Contributor
3,516

Horst once said: "You shouldn't do that but you can by using absolute type names."

Read only

0 Likes
3,514

Is there any reason why we shouldn't use absolute type names ?

Read only

matt
Active Contributor
0 Likes
3,514

Yes. It's really bad programming. Likely to lead to hard to find (and therefore extremely expensive) bugs.

Why do you think you need to use local classes in this way?

Read only

nomssi
Active Contributor
3,514

It breaks encapsulation.

  1. If you need the functionality, that copy the class in your local scope.
  2. If the class is too complex to copy, then you probably do not understand its contract, so how are your going to make sure it is created correctly and all its collaborators are also correctly defined?
  3. Even if you manage to do it, the original code is by definition out of your control and can change in unexpected ways.

So, you shouldn't, but you can.

JNN

Read only

0 Likes
3,514

Better question to ask would be "why do I have to have some functionality that is implemented as local?" In this case, ask "why" not "why not".

Read only

former_member241258
Active Participant
0 Likes
3,514

hi

you can export/import object of class in one report to another report.

for this you have to use shared memory concept technique.