5

I have an ABAP include containing only constants. (Not my code)

I want to use these constants in an ABAP OO method. (My code)

How can I use these constants in an object oriented ABAP environment without copying them?

The idea is to define these constants once and only once. And they are already defined in this include.

Additional question: is it possible to create a class that contains the constants of the above include in a public section, so that I do the include only once and use these constants in an object oriented way from other classes?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • Do you want to use the constants in your implementation only or do you need them in the class interface (e. g. method parameter default values) as well? – vwegert Feb 03 '17 at 10:24
  • I need them in the implementation. class interface would be nice, so I can use the constants from other classes - but not necessary. – Gerd Castan Feb 03 '17 at 11:26

2 Answers2

8

Assuming that the include really only contains constant definitions: From the Class Builder, select Goto --> Class-relevant Local Definitions and place an INCLUDE statement there. This should make the constants available throughout your implementation.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • 1
    So this exposes the constants locally, but is there a way to expose the constants from an include at public level using a Class so that I can call parameterised class methods with those constants? (Though perhaps I should be asking that as a separate question) – Lilienthal Dec 15 '17 at 09:58
-2
  1. Will you need these constants just for this class? Create private atributes in the class.
  2. Will you use it in other classes (and programs as well)? Create and class with static components and use it everywhere...
  • 2
    This would duplicate the constant definitions. My question is how to avoid this duplication. – Gerd Castan Feb 03 '17 at 11:28
  • Did you already try to insert the include in the class definition? Insert the include in the class definition... If you want it to use in more classes, create an abstract global class and insert the include in it. – Mauricio G Teixeira Feb 03 '17 at 12:14
  • 1
    I got a syntax error when I tried to insert the include in the class definition. This was my first idea before I posted this question. The accepted answer works. – Gerd Castan Feb 03 '17 at 12:24
  • Using Class-relevant Local Definitions must be the best option for this case, but if you will use this in other classes, maybe you should create an abstract global class for this and insert the include in it. – Mauricio G Teixeira Feb 03 '17 at 12:33