We all know when we declare a variable with the DATA statement, we can use the VALUE addition to set a default value, right ?
Like this:
What happens when you don’t specify a VALUE addition ?
Like this :
If the VALUE addition is not used, the system uses the type-specific initial value, which in case of d(date) is ‘00000000’
( Refer to the chart I have created for pre-defined data-types ).
Now that was for variable declaration, but what about constants.
If the CONSTANTS statement is used, the VALUE addition must always be specified (otherwise you will get syntax error).
What if I don’t want to provide a value to the constant but want to just use the initial value supplied by SAP? Then the addition VALUE IS INITIAL can be used.
Like this :
But here is something very interesting and which you need to keep in mind when you are programming.
Start values for Constants and Variables must match the data type used.
Only use the addition VALUE to enter start values that exactly match the data type of the declared variable in terms of type, content, and length.
Here is an example of Good and Bad ABAP Coding practice.
Bad example
Look at the following constant declaration.
If you read this code you will expect the constant lc_afternoon to contain the value 120000, right!! However, the constant will actually contain the value 092000.
What is the magic here? Why is SAP not doing what we asked for?
This is because the value of the numerical literal refers to the number of seconds. This means 12,000 seconds from the start of the day i.e 12 o’clock midnight. Which is actually the time 09:20 A.M on the following day.
Good example
The following source code corrects the above example by replacing the number literal with a text field literal. Now the constant lc_afternoon contains the expected value 120000.
—
If you liked this tip do share it.
A useful tip, regarding the TIME value . Never actually used in any case but it’s good to know its effect.
“A simple and effective blog.”
Hi Naren, Thanks for the comment.
The main point I wanted to elaborate on was that initial values of data-types can sometimes cause issues which may not be easily noticeable during testing. I have even seen production issues caused when developers don’t pay attention to initial values of data objects.