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

CONSTANT

Former Member
0 Likes
3,659

HI...

What is the use of Constant,could you give me any example coding?

regards.

Jay

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,538

Hi

The constant is a constant so a variable having a value can't be changed.

The constant are often created to became the program more clear.

Suppose you want to read the first 100 records of an internal table:

CONSTANT MAX_RECORDS TYPE I VALUE 100.

DO.
    READ TABLE ITAB INDEX SY-INDEX.
* Stop the reading if the table has less than 100 record    
    IF SY-SUBRC <> 0. EXIT. ENDIF.
* Stop the reading if the record 100 was read
    IF SY-INDEX = MAX_RECORDS.
       EXIT.
    ENDIF.
ENDDO.

In this way it's easier to understand the meanining of the control to exit from the loop.

Max

10 REPLIES 10
Read only

sourabhshah
Product and Topic Expert
Product and Topic Expert
0 Likes
2,538

hi,

1. CONSTANTS c. ... VALUE [ val | IS INITIAL ].

2. CONSTANTS c(len) ... VALUE [ val | IS INITIAL ].

3. CONSTANTS: BEGIN OF crec,

...

END OF crec.

Effect

The CONSTANTS statement defines global and local constants. By using constants, you can read statically declared data objects, since they always have a particular data type. You can use constants in any position where fields are allowed, and also in the following positions:

To specify length in a DATA STATICS, CONSTANTS, PARAMETERS, or TYPES statement.

As a DECIMALS value in a DATA, STATICS, CONSTANTS, PARAMETERS, or TYPES statement.

As an OCCURS value in a DATA, STATICS, RANGES, or TYPES statement.

In the VALUE addition of a DATA, STATICS, or CONSTANTS statement.

As an offset or length specification in a field specification.

As a value for an exception (EXCEPTIONS) in the CALL FUNCTION statement.

Constants always have a defined data type. Data types and data objects are integral parts of the ABAP type concept.

In contrast to variables defined with the DATA statement, you cannot change the value of a constant once it has been defined.

Apart from the additions ... TYPE typ OCCURS n, ... LIKE f1OCCURS n and WITH HEADER LINE, all the additions used with the DATA statement are allowed. However, in contrast to the DATA statement, the addition ... VALUE val or VALUE IS INITIAL is obligatory with variants 1 and 2. See additions with DATA.

Example

CONSTANTS CHAR1 VALUE 'X'.

CONSTANTS INT TYPE I VALUE 99.

CONSTANTS: BEGIN OF CONST_REC,

C(2) TYPE I VALUE 'XX',

N(2) TYPE N VALUE '12',

X TYPE X VALUE 'FF',

I TYPE I VALUE 99,

P TYPE P VALUE 99,

F TYPE F VALUE '9.99E9',

D TYPE D VALUE '19950101',

T TYPE T VALUE '235959',

END OF CONST_REC.

Read only

0 Likes
2,538

You define constants using the ABAP keyword CONSTANTS. Their type is defined similarly to the type

of a variable in the DATA.statement. You can assign a value using a literal, with the VALUE addition

ex-

CONSTANTS c_ua

TYPE s_carr_id

VALUE 'UA'.

READ TABLE itab

INTO wa_itab

WITH TABLE KEY

carrid = c_ua.

IF sy-subrc ne 0.

  • Message

...

ENDIF.

In this example, the system should read a line from internal table itab with key access. There is no line

with the required key at runtime. The Basis function for the READ statement is therefore terminated and

the value 4 is placed in field sy-subrc. Field sy-subrc is queried in the program immediately after the

READ statement.

Read only

Former Member
0 Likes
2,538

CONSTANTS are used in place of hardcoded values.

Take for example, you are using a value '123' in a lot of places in your program.

Later you want to change them to '456'.

It is a problem for you to change each and evry occurance of '123' with '456'.

Hence you declare a constant which is declared globally.

Ther you can simply change it at definition level. It would reflect in all other locations where it is used.

Constants: c_value(5) type c value '12345'.

Regards,

Ravi

Read only

Former Member
0 Likes
2,539

Hi

The constant is a constant so a variable having a value can't be changed.

The constant are often created to became the program more clear.

Suppose you want to read the first 100 records of an internal table:

CONSTANT MAX_RECORDS TYPE I VALUE 100.

DO.
    READ TABLE ITAB INDEX SY-INDEX.
* Stop the reading if the table has less than 100 record    
    IF SY-SUBRC <> 0. EXIT. ENDIF.
* Stop the reading if the record 100 was read
    IF SY-INDEX = MAX_RECORDS.
       EXIT.
    ENDIF.
ENDDO.

In this way it's easier to understand the meanining of the control to exit from the loop.

Max

Read only

Former Member
0 Likes
2,538

Hi Jay,

Constants are named data objects that you create statically using a declarative statement. They allow you to store data under a particular name within the memory area of a program. The value of a constant must be defined when you declare it. It cannot subsequently be changed. The value of a constant cannot be changed during the execution of the program. If you try to change the value of a constant, a syntax error or runtime error occurs.

You declare them using the CONSTANTS statement. Within the program, you can also declare local variables within procedures using CONSTANTS. The same rules of visibility apply to constants as to types (see The TYPE Addition). Local constants in procedures obscure identically-named variables in the main program. Constants live for as long as the context in which they are declared.

The syntax of the CONSTANTS statement is exactly the same as that of the DATA statement, but with the following exceptions:

You must use the VALUE addition in the CONSTANTS statement. The start value specified in the VALUE addition cannot be changed during the execution of the program.

You cannot define constants for XSTRINGS, references, internal tables, or structures containing internal tables.

Elementary constants:

CONSTANTS: pi TYPE P DECIMALS 10 VALUE '3.1415926536'.

ref_c1 TYPE REF TO C1 VALUE IS INITIAL.

The last line shows how you can use the IS INITIAL argument in the VALUE addition. Since you must use the VALUE addition in the CONSTANTS statement, this is the only way to assign an initial value to a constant when you declare it.

Complex constants:

CONSTANTS: BEGIN OF myaddress,

name(20) TYPE c VALUE 'Fred Flintstone',

street(20) TYPE c VALUE 'Cave Avenue',

number TYPE p VALUE 11,

postcode(5) TYPE n VALUE 98765,

city(20) TYPE c VALUE 'Bedrock',

END OF myaddress.

This declares a constant structure MYADDRESS. The components can be addressed by MYADDRESS-NAME, MYADDRESS-STREET, and so on, but they cannot be changed.

regards,

keerthi

Read only

Former Member
0 Likes
2,538

1. CONSTANTS c. ... VALUE [ val | IS INITIAL ].

2. CONSTANTS c(len) ... VALUE [ val | IS INITIAL ].

3. CONSTANTS: BEGIN OF crec,

END OF crec.

Constants are been used as holders. Say, in a program 'ABCD' needs to be used at about 20 places, then u can declare it as a constant and use it. It if good program effieciency.

Read only

Former Member
0 Likes
2,538

In addition to the above replies ,

whenever u want to change the value of a constant , it will be easy for u to change at one place rather than changing the value in the whole program

1.Say u have to use the value 'SDN' , to compare some string 100 times in your program

eg : if URL eq 'SDN'. " Say u have to use th e same statement 100 times in ur program in different situaltions, if u use like this it will be difficult whenever u want change the value SDN to SAP SDN

2.If u declare a constant u can change the value from SDN to SAP SDN at one time only

Read only

0 Likes
2,538

Hi chandirasekhar...

please give some example to me

Read only

0 Likes
2,538

1 . without using constants

Say you have to Compare the name JAY 10 times

IF NAME EQ 'JAY'.

******

ENDIF.

IF NAME EQ 'JAY'.

******

ENDIF.

And so on till 10 times...............

Now you want to change the name JAY to RICH

Now what you have to do is change the name 10 times in all the IF conditions

2. Using CONSTANTS

CONSTANTS : C_NAME(10) value 'JAY'.

IF NAME EQ C_NAME.

******

ENDIF.

IF NAME EQ C_NAME.

******

ENDIF.

and so on till 10 times

Now when u want to change the name from JAY to RICH u can change the value in constant instead of change all the IF Conditions

CONSTANTS : C_NAME(10) value 'RICH'. " change the value here

Read only

Former Member
0 Likes
2,538

We use CONSTANTS if the value of variable never changes throughout the program and will be used multiple times.

Instead of hardcoding such values multiple times, we declare variable as CONSTANT.