The difference between the static variables of Java and global and local variables

Source: Internet
Author: User

The difference between a static variable and a global, local variable


A static global variable is formed by the description of the global variable (external variable), preceded by static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.


From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.


The static function differs from the normal function scope. Only in this document. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to which the function is to be used is to contain the header file


What is the difference between a static global variable and a normal global variable: the static global variable is only initialized once, preventing it from being referenced in other file units;

What is the difference between a static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the last result value;

What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call


The local variables of the program exist in (the stack), the global variables exist in the (static zone), and the dynamic requisition data exists in the (heap).


The extern global variable (the extern-modified variable simply means that the variable is defined elsewhere, so it must be defined in other places with an explicit definition such as int A, and cannot be modified with static), the lifetime of the static global variable and the static local variable is "permanent", The difference is only the visible fields are different. The extern global variable visible area is a project, the static global variable visible area is a file, and the visible area of the static local variable is a block.

From a code maintenance perspective, modifications to the extern variable may affect all code, and modifications to the static global variable may affect the code in one file, and modifications to the static variable may affect the code of a block, so the priority is the static partial when the variable type is selected > Static global >extern Global. But they have common drawbacks: functions that use these types of variables will be non-reentrant, not thread-safe. There are many functions in the C + + standard library that use static local variables, and the current implementation provides them with two sets of code, a single-threaded version using a static variable and a multithreaded version that uses "Thread global variables," such as Rand,strtok.


A process free memory space of 4G, can be divided into the storage of static data, code, system memory, heap, stack and so on: the activity records generally hold the call parameters, return address and so on. The biggest difference between heaps and stacks is that the heap is allocated memory from a low address to a high address, while the stack is from high to low. Global and static data is stored in the global data area, and the rest is in the stack, with the memory allocated by malloc or new in the heap. Generally the stack is at a low address, and the heap is located high.

The full use of static

To understand static, you must first understand another keyword relative to it, many people may not know that there is this keyword, that is, auto, in fact, we usually declare the variable without static modification, is auto, because it is the default, Just as short and long always default to int; we usually declare a variable:

int A;

string S;

is actually:

auto int A;

Auto string s;

The declaration of the static variable is:

static int A;

static string S;

This seems to be more conducive to understanding that auto and static are pairs of key words, like private,protected,public;

For static does not understand, in fact, is not understanding of auto, because it is more general, and some things you use every day, but not necessarily on behalf of you really understand it; auto means that the lifetime of the variable is automatically controlled by the program, which usually means that the variable is assigned when it enters its scope. is released when it leaves its scope, and static is not auto, and the variable is allocated at program initialization until the program exits, that is, Static is assigned to release the variable according to the program's life cycle, not the variable's own life cycle; So, an example like this:

void Func ()

{

int A;

static int b;

}

Each time the function is called, the variable A is new because it is allocated when it enters the body of the function and is freed when it exits the body of the function, so multiple threads invoke the function, each of which has its own independent variable A, because it is always reassigned, and the variable b whether or not you use the function, It is assigned when the program is initialized, or when it is first executed to its declaration (different compilers may be different), so when multiple threads invoke the function, they always access the same variable B, which must be noted in multithreaded programming!

All uses of static:

1. Static members of the class:

Class A

{

Private

static int s_value;

};

It must be initialized in the CPP:

int a::s_value = 0; Note that there is no static modification!

A static member of a class is a common member of all instances of the class, that is, a global variable within the scope of the class, or a global variable named A::s_value, except that it is a class-safe attribute; it is simple because it is allocated at the time of program initialization, so it is allocated only once, So it is shared;

The static member of the class must be initialized, the same is true, because it is allocated at the time of initialization of the program, so there must be initialization, the class is only declared, in the CPP is the initialization, you can put a breakpoint on the initialization code, before the program executes the first statement of main will go to that If your static member is a class, it will be called to its constructor;

2. Static functions of the class:

Class A

{

Private

static void func (int value);

};

There is no need for static modification when implementing, because static is a declarative keyword;

The static function of a class is a global function within the scope of the class, cannot access the private members of the class, can only access the static members of the class, does not require an instance of the class to invoke; in fact, it is a global function that adds access to the class: void A::func (int);

A static member function of a class can inherit and overwrite, but cannot be a function that requires it;

3. Global variables that are valid only within the CPP:

Declared in the global scope of the CPP file:

static int g_value = 0;

The meaning of this variable is valid within the CPP, but other CPP files cannot access the variable, and if two CPP files declare global static variables with the same name, they are actually two independent variables;

If you do not declare a global variable using static:

int g_value = 0;

There is no guarantee that this variable will not be shared by other CPP, nor can it be guaranteed to be shared by other CPP, because to allow multiple CPP to share a global variable, it should be declared as extern (external), or the compiler may report that the variable is defined repeatedly; The use of this global variable is not clear;

If declared in a header file:

static int g_vaule = 0;

A global variable is created for each CPP that contains the header file, but they are all independent, so it is not recommended to do so, as it is not clear how to use this variable, because it creates a set of variables with the same name and different scopes;

Here, by the way, how to declare all the global variables that CPP can share, declared as extern in the header file:

extern int g_value; Note, do not initialize the value!

It is then initialized (once) in any of the CPP containing the header file:

int g_value = 0; Initialization is not an extern modification, because extern is also a declarative keyword;

Then all CPP files containing the header file can access the same variable with the name G_value;

4. Global functions that are valid only within the CPP:

Declare within CPP:

static void Func ();

The implementation of the function does not require static modification, then this function can only be used within the CPP, and will not conflict with the same name function in other CPP, and if the use of static will cause problems and the 3rd; Do not declare static global functions in the header file. Do not declare a non-static global function in the CPP, if you want to reuse the function in multiple CPP, it will refer to the header file, otherwise it is necessary to add static modification inside the CPP, which is important in C language!

5. Local Static variables:

void Func ()

{

static int s_value = 0;

}

A static variable within its scope, which accesses the same variable in multithreaded situations, retains the memory of the static variable because the function call ends, and thus can be used as the location for some dynamic variable return values:

const char * func ()

{

string s = "ABCD";

static Char msg[256];

strcpy (msg, s);

return msg;

}

Cannot directly return s, because it is dynamic; otherwise it will result in a classic memory problem (don't let me elaborate), usually we return a constant string: Return "ABCD", in fact, because "ABCD" is static char[] type (so there is no problem);

The difference between the static variables of Java and global and local variables

Related Article

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.