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 get all class elements?

1,128

Hello,

How to get all elements from class.

I want to clear all global variables. But I don't know, how I get all global elements from my Abap class.

I hope, you could help me.

19 REPLIES 19

pokrakam
Active Contributor
0 Kudos
604

An ABAP class doesn't have any global variables. All attributes are local to the class.

RTTI services can provide you all components of a class, use CL_ABAP_CLASSDESCR.

0 Kudos
604

and how i could clear all these components or is it not possible?

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
604

Loop over the attribute table, dynamic assign to field symbol, CLEAR field symbol. Basic stuff.

0 Kudos
604

How I get these attribute table. I don't manually used the CL_ABAP_CLASSDESCR before.

pokrakam
Active Contributor
0 Kudos
604

If you haven't done this before, then I would ask why you think you need to do it this way?

See answers from Uwe and Suhas.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
604

Have you even looked at it?

horst_keller
Product and Topic Expert
Product and Topic Expert
604

CL_ABAP_CLASSDESCR

Loop over the attribute table, dynamic assign to field symbol (attribute must be visible), CLEAR field symbol.

matt
Active Contributor
604

If you mean how to clear all statics in a class - write a method to do it and call that.

0 Kudos
604

I want to do this dynamic and don't want to clear every variable by hand.

matt
Active Contributor
604

Clearing all the static attributes of a class indicates that your design is poor. I can't see why the complexity of doing it dynamically could be preferable. I've never written a class where this was needed, and I've written thousands.

Consider using the singleton pattern instead. Then you can have a static method that creates and new instance, and returns that (clearing the old one).

UweFetzer_se38
Active Contributor
604

I'd just create a new reference, let the garbage collector do the rest for you...

(after clearing all public and private variables the whole class is initial, isn't it? Or am I missing something?)

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
604

OK for instance attributes. But I think the OP means static attributes by saying global variables.

0 Kudos
604

Then I would use a structure instead of single variables.

Dynamic programming just to clear variables would be "mit Kanonen auf Spatzen schießen" IMO. But possible of course.

horst_keller
Product and Topic Expert
Product and Topic Expert
604

If it's your class ...

0 Kudos
604

Does that mean a public instance attribute is less global than a private static attribute?

Is there a globality ranking published somewhere I can use for future reference? 😉

horst_keller
Product and Topic Expert
Product and Topic Expert
604

Independently from what the OP meant, you cannot clear all attributes by garbage collection but only instance attributes. Since he said "all elements from class", it is justified to assume that he meant all attributes including static attributes.

The terms for your reference are defined in the ABAP Glossary, e.g. global data, which, alas, are seldom use here .

0 Kudos
604

It was a joke. You assumed he meant static, but if someone inaccurately calls attributes global, which they clearly aren't (as I said in my first response), then they would not necessarily have the same view.

If we try to define the globularity (globulosity?) of scope vs lifetime, one could argue that that which is visible to the outside looks 'global', making a public 'more global' than a private static, even though it has a shorter lifetime.

But, just in case someone draws any conclusions, all of this is nonsense of course 🙂

SuhaSaha
Advisor
Advisor
604

@OP: What is your motivation for clearing the attributes of the instance?

matt
Active Contributor
604

Your question is bit poor. Rather than ask how to implement your chosen solution to the programming problem, you should explain what you are trying to achieve in general. Then you may get other solutions that are better than your preferred one (dynamic clearing of static attributes, it seems). Furthermore, it means people don't have to guess at your intentions, thereby increasing the quality of the answers.