Class A
{
int a;//member Variable
public static void Main (string[] args)
{
int b;//local variable
}
}
————————————————————————————————————————————————————————————————————————————————————————
The class body is composed of 2 parts:
Part is the definition of the variable;
A part is the definition of a method (there can be more than one method in a class) the variable defined in the variable definition section is called a member variable of the class, and the member variable is valid throughout the class. A variable defined in a method body is called a local variable, and a local variable is only valid in the method in which it is defined. Member variables are also divided into instance variables and class variables (static static variables).
Class One
{
float x; X is an instance variable
static int y; As long as the keyword Static,y as a class variable
}
————————————————————————————————————————————————————————————————————————————————————————
1. Member variables can be decorated with modifiers such as public,protect,private,static, while local variables cannot be controlled modifiers and static modifiers; both can be defined as final
2. Member variables are stored in the heap, local variables are stored in the stack
3. Time of existence is different
4. The member variable has a default value (it must be explicitly assigned if it is final decorated and does not have static), and the local variable does not automatically assign a value
————————————————————————————————————————————————————————————————————————————————————————
Let's look at the following two procedures:
Program One:
public class Variable
{
int i;
void Test ()
{
int j=8;
if (j==i)
System.out.println ("equal");
Else
System.out.println ("unequal");
}
public static void Main (string[] args)
{
Variable v=new Variable ();
V.test ();
}
}
Program Two:
public class Variable
{
void Test ()
{
int i;
int j=8;
if (j==i)
System.out.println ("equal");
Else
System.out.println ("unequal");
}
public static void Main (string[] args)
{
Variable v=new Variable ();
V.test ();
}
}
----------------------------------------------------------------------------------------------------------
The first program is normal and compiles without errors. The second program compiles with the following error:
D:\program\java\test>javac Variable.java
Variable.java:9: The variable I may not have been initialized
if (j==i)
^
1 Error
This error occurs because the member variable has a default value (it must be explicitly assigned without static), and the local variable is not automatically assigned a value
===========================================================
The class body is divided into two parts. Variables defined in the variable definition section are called member variables of the class, and the parameters of variables and methods defined in the method body are called local variables
The difference between a member variable and a local variable in Java
1. Member variables can be decorated with modifiers such as public,protect,private,static, while local variables cannot be controlled modifiers and static modifiers; both can be defined as final
2. Member variables are stored in the heap, local variables are stored in the stack
3. Time of existence is different
4. The member variable has a default value (it must be explicitly assigned if it is final decorated and does not have static), and the local variable does not automatically assign a value
-------------------------------------------------------------------------------------------------------------
As implies the idea.
What is local, inside the method, the inside of the block is local, the execution instruction exits that local, the local variable clears automatically
such as the method, needless to say
In the Block
{
int i=0; This is a local variable, and its scope is within it.
}
Member variables will be involved in WHO members, members of the class? Member of the instance?
Class a{
int i=0; Member of the instance
Static members of the j=1;//class
}
Static difference, whether it depends on the instance and exists
----------------------------------------------------------------------------------------------------------
Member variable: exists as a member of a class and exists directly in the class.
Local variable: exists as a member of a method or statement block, and exists in the parameter list and method definition of a method.
Local variables must be actively initialized by the programmer before use, and in contrast, the member variables in the system will be provided with a default initial value. So in syntax, the member variables of a class can be defined and used directly, and local variables are assigned initial values before they are defined.
Member variables for all classes can be referenced by this. Member variables are also divided into two types: instance variables and static variables. Static variables are defined with the static keyword.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +
The type descriptor for a static variable is static. Static variables, of course, are static storage, but the amount of static storage is not necessarily static variables, such as external variables are static storage, but not necessarily static variables, must be defined by static to be static external variables, or static global variables. For automatic variables, it belongs to the dynamic storage mode. But you can also use static to define it as a static automatic variable, or a static local variable, to become a static storage mode.
As a result, a variable can be re-described by static and change its original storage mode.
1. Static local Variables
A static local variable is formed by adding the static specifier before the description of the local variable.
For example:
static int A, B;
static float array[5]={1,2,3,4,5};
Static local variables belong to static storage, which has the following characteristics:
(1) A static local variable is defined within a function, but not as an automatic variable, when it is called and disappears when the function is exited. A static local variable always exists, that is, its lifetime is the entire source program.
(2) The lifetime of a static local variable is the entire source program, but its scope is still the same as the automatic variable, that is, the variable can only be used within the function that defines it. After exiting the function, it cannot be used even though the variable continues to exist.
(3) allows the initial value to be assigned to the static local quantity of the construction class. If the initial value is not assigned, the system automatically assigns 0 values.
(4) If the static local variable of the basic type is not assigned the initial value in the description, the system automatically assigns 0 values. The value of the automatic variable is indeterminate if it is not assigned the initial values. According to the characteristics of static local variables, it can be seen that it is a lifetime of the whole source of the program amount. Although it cannot be used after the function that defines it, it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved. Therefore, you can consider static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects, so it is advisable to use local static variables
—————————————————————————————————————————————————————————
Give the reader a straightforward example (distinguishing between static local variables and dynamic local variables):
——————————************************************************************——————————
int fun (int n)
{
static int f=1; */* Please note the modification of this line */This is the static result:
F=f*n;
return (f);
}
Main ()
{
int i;
for (i=1;i<=5;i++)
printf ("%d!=%d\n", I,fun (i));
}
——————————************************************************************——————————
int fun (int n)
{
int f=1; */* Please note the modification of this line * * results are:
This is the result of auto.
F=f*n;
return (f);
}
Main ()
{
int i;
for (i=1;i<=5;i++)
printf ("%d!=%d\n", I,fun (i));
}
————————————————————————————————————————————————————————————————
2. Static global variables
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. So the function of the static descriptor is different in different places. Should be taken into consideration.
static variables
In addition to the scope, variables have a survival period, in which the variables retain their values. The values of module-level variables and common variables are maintained throughout the lifetime of the application. However, for Dim declared local variables and for declaring local variables, these local variables exist only when the procedure is executing. Typically, when a procedure is executed, the value of its local variable is no longer present, and the memory occupied by the variable is freed. The next time the procedure is executed, all its local variables are reinitialized.
However, a local variable can be defined as static, thereby preserving the value of the variable. Declare one or more variables inside the procedure with the Static keyword, with the same usage as the Dim statement:
Static Depth
For example, the following function adds the previous operations value stored in the static variable accumulate to a new one to calculate the total operating value.
Function runningtotal (num)
Static ApplesSold
ApplesSold = ApplesSold + num
RunningTotal = ApplesSold
End Function
If you use Dim instead of Static to declare ApplesSold, the previous cumulative value is not preserved by the calling function, and the function simply returns the same value that it was called.
Declare ApplesSold in the Declaration section of the module and make it a module-level variable, and you will receive the same effect. However, once this method changes the scope of the variable, the process is no longer exclusive access to the variable. Because other processes can also access and change the value of a variable, the operating value may be unreliable and the code will be more difficult to maintain.
Declares that all local variables are static variables
To make all local variables in a procedure static, you can add the static keyword at the beginning of the process header. For example:
Static Function runningtotal (num)
This makes all local variables in the procedure static, whether they are declared with static, Dim, or Private, or implicitly. You can put Static in front of any Sub or Funtion procedure header, including event procedures and procedures declared as Private.
————————————————————————————————————————————————————————————————————————————————————————
1. Definition of dynamic storage mode and static storage mode
1) The so-called static storage means to allocate a fixed amount of storage space during program compilation;
2) The so-called dynamic storage means to dynamically allocate storage space as needed during the program's operation.
2. Allocation of user storage space in memory (divided into three parts)
1) Program Area: Store Program statement
2) Static storage: The global variable is stored, and when the program starts execution, the global variable is allocated a storage area, and the program executes and is released.
3) Dynamic Storage area:
The following data is stored:
① function Form parameter. Allocating storage space to parameters when calling a function;
② automatic variables (local variables without static declarations);
③ the field protection and return address when the function is called.
Stacks are stores of variables that are allocated by the compiler when needed, and that are automatically clear when not needed. The variables inside are usually local variables, function parameters, and so on.
Heap, which is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes.
What is the difference between a member variable and a local variable?