Skip to Content
0

Local class # Object creation from another program

Feb 20 at 08:06 AM

129

avatar image

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

??
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Jacques Nomssi Feb 20 at 08:46 AM
2

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

Show 4 Share
10 |10000 characters needed characters left characters exceeded

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

0
Arun Kumar Gopinathan

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?

0
Arun Kumar Gopinathan

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

3
Arun Kumar Gopinathan

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".

0
Tomas Buryanek Feb 20 at 08:12 AM
2

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 :)

Show 7 Share
10 |10000 characters needed characters left characters exceeded
3

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.

2
Matthew Billingham

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

1

Edited, thank you.

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

0

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". :)

1
Jelena Perfiljeva

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 ;-)

0

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. ;-)

1
Mike Pokraka Feb 20 at 08:30 AM
2

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_guidl.htm

Share
10 |10000 characters needed characters left characters exceeded
Krishna Murthy Feb 21 at 05:16 AM
-5

hi

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

for this you have to use shared memory concept technique.

Share
10 |10000 characters needed characters left characters exceeded