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

Former Member
4,614

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
Read only

pokrakam
Active Contributor
0 Likes
4,090

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.

Read only

Former Member
0 Likes
4,090

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

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
4,090

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

Read only

Former Member
0 Likes
4,090

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

Read only

pokrakam
Active Contributor
0 Likes
4,090

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.

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
4,090

Have you even looked at it?

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
4,090

CL_ABAP_CLASSDESCR

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

Read only

matt
Active Contributor
4,090

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

Read only

Former Member
0 Likes
4,090

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

Read only

matt
Active Contributor
4,090

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

Read only

UweFetzer_se38
Active Contributor
4,090

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

Read only

0 Likes
4,090

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

Read only

0 Likes
4,090

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.

Read only

4,090

If it's your class ...

Read only

0 Likes
4,090

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? 😉

Read only

4,090

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 .

Read only

0 Likes
4,090

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 🙂

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
4,090

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

Read only

matt
Active Contributor
4,090

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.