User-Defined Variable Declaration and Scope
The difference between the variable declarations concerns their life spans:
-
Global
The variable is valid for the entire session.
Global variables can be accessed from different components and applications, including an Application Engine program. A global variable must be declared, however, in each PeopleCode program where it’s used. Use global variables rarely, because they are difficult to maintain.
Global variables are not available to a portal or applications on separate databases.
-
Component
Component variables remain defined and keep their values while any page in the component in which they are defined remains active. Similar to a global variable, a component variable must be declared in each PeopleCode program where it is used.
Component variables remain defined after a TransferPage, DoModal, or DoModalComponent function. However, variables declared as Component do not remain defined after using the Transfer function, whether you are transferring within the same component or not.
Component variables act the same as global variables when an Application Engine program is called from a page (using CallAppEngine).
-
Local
The variable is valid for the duration of the PeopleCode program or function in which the variable is defined.
Local variables declared at the top of a PeopleCode program (or within the main, that is, non-function, part of a program) remain in scope for the life of that PeopleCode program. Local variables declared within a method or function are valid to the end of the method or function and not beyond.
You can explicitly declare variables using the Global, Component, or Local statements, or you can use local variables without declaring them. Here are some examples of explicit variable declarations:
Local Number &AGE;
Global String &OPER_NICKNAME;
Component Rowset &MY_ROWSET;
Local Any &SOME_FIELD;
Local ApiObject &MYTREE;
Local Boolean &Compare = True;
Component PTFP_FEED:FeedFactory &cFeedFactory;
Variable declarations are usually placed above the main body of a PeopleCode program (along with function declarations and definitions). The exception is the Local declaration, which you can use within a function or the main section of a program. You can declare variables as any of the PeopleCode data types. If a variable is declared as an Any data type, or if a variable is not declared, PeopleTools uses an appropriate data type based on context.
Note:
Declare a variable as an explicit data type unless the variable will hold a value of an unknown data type.
You can check the values of Global, Component, or Local variables at runtime in the different variable windows of the PeopleCode debugger. Local variables declared within a function appear in the Function Parameters window.