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

constants

Former Member
0 Likes
4,152

How to declare constants in the program

9 REPLIES 9
Read only

Former Member
0 Likes
2,490

constants: c_value(1) value '1'.

Read only

Former Member
0 Likes
2,490

hi,

constants : c_var type (give any datatype) value '(give a constant value)'.

Regards,

Shrita.

Message was edited by: shrita sharma

Read only

Former Member
0 Likes
2,490

Hi,

Go thru this Docu.

CONSTANTS

Syntax

CONSTANTS const [options].

Effect

This statement declares a constant data object - that is, a constant const. The content of a constant cannot be changed at runtime of an ABAP program. You can only use it as an operands in read positions of ABAP statements. Constants that you declare in the declaration section of a class or an interface belong to static attributes of that class/interface.

The name conventions apply to the name const. The syntax of the additions options of the CONSTANTS statement, for declaring constants, corresponds to that of the DATA statement for declaring variables. The only differences are that the READ-ONLY addition is not possible and within the declaration of a structure, the INCLUDE statement cannot be used.

In contrast to the DATA statement, you have to specify an Startwert with the addition VALUE using the CONSTANTS statement. The same restrictions as for the DATA statement apply. This has the following implications for the declaration of constants:

You cannot declare any constants with a deep data type prior to release 6.10. As of release 6.10 and later, you can also declare deep constants, but you can only specify a start value val for ABAP type string. To assign an initial values to the other deep constants, you have to use IS INITIAL.

Constant internal tables are always empty, and prior to release 6.40, you can only declare them by referring to an existing table type and you cannot declare any new bound table types (see under DATA - TABLE OF), because you cannot use the addition VALUE in this case. As of release 6.40, the VALUE addition can also be specified when defining internal tables. Therefore, you can create new bound table types using the CONSTANTS statement.

Note

If you use the class component selector, you can also use the interface name to access static attributes of interfaces that were declared using CONSTANTS.

Example

The statements below declare a numeric constant, a constant structure and a constant reference. You can use the reference in comparisons, for example, or pass it on to procedures.

CONSTANTS pi(8) TYPE p DECIMALS 14

VALUE '3.14159265358979'.

CONSTANTS: BEGIN OF sap_ag,

zip_code(5) TYPE n VALUE '69189',

city TYPE string VALUE `Walldorf`,

country TYPE string VALUE `Germany`,

END OF sap_ag.

CONSTANTS null_pointer TYPE REF TO object VALUE IS INITIAL.

Thanks

Sunil

Read only

Former Member
0 Likes
2,490

Can i declare constants for list heading like name date time if so how

Read only

0 Likes
2,490

create text elements

just for ex :

itab-field = text-001.

Regards

- Gopi

Read only

0 Likes
2,490

Yes, you can do that but the best practise would be to use the List headings option under the menu

Goto -> Text Elements -> List Headings.

If that does not suit your requirement use Text Elements.

Read only

Former Member
0 Likes
2,490

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 VALUEaddition. 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 TYPE c LENGTH 20 VALUE 'Fred Flintstone',

street TYPE c LENGTH 20 VALUE 'Cave Avenue',

number TYPE p VALUE 11,

postcode TYPE n LENGTH 5 VALUE 98765,

city TYPE c LENGTH 20 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.

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 constants 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 apply to the visibility of data types. Local constants in procedures obscure identically-named variables in the main program. Constants exist 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.

Read only

Former Member
0 Likes
2,490

Hi,

It is always a good practise to write keywork in ABAP editor and press F1.

This will give SAP Documenatation with example.

This will help you in future in any case.

So in your current case ...

type CONSTANTS and press F1 on that.

Check whether it satisfies your doubts.

*Please mark all informative responses.

Read only

Former Member
0 Likes
2,490

Hello,

check this out.

constants : c_var <b>type</b> (give any datatype) <b>value</b> '(give a constant value)'.

example,

- date type sy-datum value '20061018'.

- int type i value 10.

Regards,

Shehryar