当前位置:网站首页>Explain in detail the three types of local variables, global variables and static variables

Explain in detail the three types of local variables, global variables and static variables

2022-06-26 00:03:00 Little fish like to eat vegetables

It's better to do than not , Better finish than delay
Today I want to talk about the knowledge of local variables , Because I just turned to this topic today , I happened to collect some information , So I wanted to talk about .

1. local variable

First , Here's what I'm going to say , Local variables are defined in a function , It is only valid in this function , For example, let's take a look at such an example

public class jichu {
    
      void a(){
    
          String b=" Liu San ";
      }
    public static void main(String[] args) {
    
        jichu b1=new jichu();
        b1.a();
        System.out.println(b);
    }
}

In this , Let me show you the screenshot again
 Insert picture description here
What is the cause of this , Because I define local variables in the method body , So you can't use it in the global context at this time , Now you should be able to understand local variables ?

2. Global and static variables

 Insert picture description here
This refers to the static variable symbol static To set , So here you may think , I remember that global variables also operate outside the method body , Can you tell us the difference between the two ?

Global variables ( External variables ) Before the description of static It's a static global variable . Global variables themselves are static storage ,
Static global variables are also static storage . There is no difference between the two in the way of storage . The difference between the two is that the scope of non static global variables is the whole source program , When a source program consists of multiple source files , Non static global variables are valid in all source files . Static global variables limit their scope ,
That is, it is only valid in the source file where the variable is defined ,
It 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 , Can only be used for functions within the source file , So you can avoid causing errors in other source files . As can be seen from the above analysis ,
Changing a local variable to a static variable changes its storage mode, that is, its lifetime . Changing a global variable to a static variable changes its scope , It limits its use .

In the middle of it , In fact, static variables refer to two settings in the method body , Another is that it can be used in the whole class .

1. The scope of a static variable depends on its position , If in a function , Is a static local variable , The scope is this function ;
2. Static global variables , Only in this document can , Although the whole program contains multiple files , But static global variables can only be used in the file that defines them , But it can't be used in other files in the program . It is an external variable that defines the storage factor as static , Its scope is from the definition point to the end of the program , The difference is that the storage type determines the storage location , Static variables are stored in the data area of memory ,
They allocate fixed bytes before the program starts running , The size of the bytes allocated during program operation does not change , Only when the program is finished , To release the occupied memory .

3. summary

  1. Both global variables and static variables allocate space in the static storage area , Local variables are stored on the stack , Because the method area is in the stack .
  2. Global variables 、 The lifetime of a static variable is the same as that of a program , After the program ends, the operating system reclaims space .

Okay , This is the end of this issue , Minghuai, I'm going to have dinner , I'll see you next time .

原网站

版权声明
本文为[Little fish like to eat vegetables]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252111509026.html