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

report

Former Member
0 Likes
791

hi all,

what is the difference between variables & constants? explain ex. briefly?

thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
767

Hi,

Variable values can change .

In Constants u can't change value .

Data : v_val type c value 10.

LOOP AT <internal table >

-


-


V_Val = V_Val + 1.

Endloop.

Variable values can change in program execution,but constant's not.

Give reward if useful

Regards,

Narasimha

8 REPLIES 8
Read only

Former Member
0 Likes
767

HI,

Variables

Variables are named data objects that you can declare statically using declarative statements, or dynamically while a program is running. They allow you to store changeable data under a particular name within the memory area of a program.

You can declare variables statically using the following statements:

· DATA: To declare variables whose lifetime is linked to the context of the declaration

· STATICS: To declare variables with static validity in procedures

· CLASS-DATA: To declare static variables within classes

· PARAMETERS: To declare elementary data objects that are also linked to an input field on a selection screen

· SELECT-OPTIONS: To declare an internal table that is also linked to input fields on a selection screen

· RANGES: To declare an internal table with the same structure as in SELECT-OPTIONS, but without linking it to a selection screen.

This section explains the DATA and STATICSstatements. For further information about CLASS-DATA; refer to Classes. For further information about PARAMETERS, SELECT-OPTIONS and RANGES, refer to Selection Screens.

Declaring Variables Dynamically

You can also create data objects dynamically when you call procedures. These data objects are the formal parameters of the interface definition, which only have technical attributes when they inherit them from the actual parameters passed to them.

Static Variables in Procedures

Variables that you declare with the DATA statement live for as long as the context in which they are defined. Therefore variables in an ABAP main program exist for the entire runtime of the program, and local variables in procedures only exist for as long as the procedure is running.

To retain the value of a local variable beyond the runtime of the procedure, you can declare it using the STATICS statement. This declares a variable with the lifetime of the context of the main program, but which is only visible within the procedure.

The first time you call a subroutine or function module, the corresponding main program is always loaded into the internal session of the calling program. It is not deleted when the procedure ends. This enables variables defined using STATICSto retain their values beyond the runtime of the procedure, allowing them to be reused the next time the procedure is called (see the example in the Local Data in Subroutines section).

In methods, variables defined with STATICS are static attributes that are only visible in the corresponding method, but for all instances of a class (see Classes).

The syntax of the STATICS statement is identical to that of the DATA statement.

Constants

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.

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.

Read only

prasanth_kasturi
Active Contributor
0 Likes
767

hi

variables value can be changed during the run time.

constants value cannot be changed during the run time. if you change a run time error ocuurs and lead to dump.

variables are declared using data statement

constants using constants statement

consatnts : CONSTANTS: pi TYPE p DECIMALS 10 VALUE '3.1415926536'.

variables : data : v_var type i.

you can assign any integer value to v_var and change it during run time but pi value can only be used but cannot be changed

REWARD IF HELPFUL

PRASANTH

Read only

Former Member
0 Likes
767

Hi

Variables are data objects whose contents can be changed using ABAP statements. You declare variables using the DATA, CLASS-DATA, STATICS, PARAMETERS, SELECT-OPTIONS, and RANGESstatements.

Constants are data objects whose contents cannot be changed. You declare constants using the CONSTANTSstatement.

Reward If Helpfull

Naresh

Read only

0 Likes
767

you didn't give answer to my question.

Read only

0 Likes
767

thanks.

Read only

Former Member
0 Likes
768

Hi,

Variable values can change .

In Constants u can't change value .

Data : v_val type c value 10.

LOOP AT <internal table >

-


-


V_Val = V_Val + 1.

Endloop.

Variable values can change in program execution,but constant's not.

Give reward if useful

Regards,

Narasimha

Read only

Former Member
0 Likes
767

Hi,

Variable is a container which store what ever is assaigned to that and this value can change whenver we assaign other value to that.

but constant value we can't change.

for constant the declaration will be like ,

CONSTANTS: c1 type i value '2'.

variable declaration will be like,

data: v1 type i.

Regards,

kk.

Read only

Former Member
0 Likes
767

Constants:

variable:

Reward points..