This is a follow-up to my last post, where I talked about central classes to keep type declarations for an application.
Of course, I do the same thing for constants. I have for instance a class to keep some very central date constants:
class ZCL_ABAP_DATE_CONSTANTS definition public final create public . public section. constants maximum_date type d value '99991231'. constants first_day_of_year type string value '0101'. constants last_day_of_year type string value '1231'. constants minimum_date type d value '00010101'. protected section. private section. ENDCLASS.
Unfortunately things do not work as they do for type classes. I found out, that you can access constants of a class only using the public class and the static operator =>:
* Working call_method( zcl_abap_date_constants=>maximum_date ).
However, if I create a short cut using a variable, it doesn’t work (although this works for the types – strange):
data date_constants type ref to zcl_abap_date_constants. data common_types type ref to zcl_abap_common_types. " Working for types data binary_table type common_types->binary_table. " Not working for constants --> syntax error call_method( date_constants->maximum_date ).
A workaround could be instantiate the class using the variable:
class app definition. public section. methods constructor. methods main. private section. data date_constants type ref to ZCL_ABAP_DATE_CONSTANTS. endclass. class app implementation. method constructor. date_constants = new #( ). endmethod. method main. cl_demo_output=>display( date_constants->maximum_date ). endmethod. endclass.
But as Maxim Freck pointed out, it’s also possible to keep the constant class abstract and inherit a local class from it:
class constants definition final inheriting from zcl_abap_date_constants. endclass. * Working call_method( constants=>maximum_date ).