Static and global variables

Source: Internet
Author: User

Static and global variables

Static variablesJava is also called a class variable. It can be seen from the name that it belongs to a class rather than a class instance. Its keyword is static storage in the static storage area. When the program is executed and the class code is loaded into the memory, the static variables of the class are allocated with memory space, it belongs to the class, that is, no instance object is for a static variable. The memory space of the static variable knows that the program exits to release the occupied memory space. Static variables can be accessed directly by class names (Java can also be accessed by object names ).

 Some people will divide the Global static variables and local static variables. The Global static variables lose the general meaning of "global" and their scope is limited in this file. The declaration of static variables should appear in functions of classes rather than classes.

 

Global VariablesThe keyword Global is also a static storage method, which is stored in the static storage area. Global variables can be defined in only one source file to act on all source files. Of course, other source files that do not contain the definition of a global variable must use the extern keyword to declare the global variable again.

 

 

Differences between global variables and static variables:

 Global variables are valid throughout the project file;The static global variable is valid only in the file that defines it.;Static local variables are valid only in the defined function, but the program only allocates memory once. After the function returns, the variable does not disappear.; The local variable is valid in the defined function, but fails after the function is returned. The description of global variables (external variables) is preceded by static to form a static global variable.Global variables are static storage, and static global variables are also static storage.. The two are not different in storage methods. The difference between the two lies inThe scope of non-static global variables is the entire source program.,When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program.Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

 

 It can also be viewed as follows:

 A. If the global variable is only accessed in a single file, you can change the variable to a static global variable to reduce the coupling between modules;

 B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;

 When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import:

 D. If we need a reentrant function, we must avoid using static variables in the function (such a function is called a function with the "internal memory" function)

 E. static variables must be used in a function. For example, if the return value of a function is of the pointer type, the address of the static local variable must be used as the return value. If the return value is of the auto type, the returned result is an error pointer.

 

From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places. Attention should be paid.

 

Six functions of Variables: Global scope, File Scope, namespace scope, Class scope, local scope, and statement scope.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.