当前位置:网站首页>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 .
List of articles
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 
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

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
- 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 .
- 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 .
边栏推荐
- DHCP review
- MySQL InnoDB lock knowledge points
- The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
- 推荐系统设计
- 使用npm创建并发布包时遇到的常见问题
- Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
- 文獻調研(三):數據驅動的建築能耗預測模型綜述
- 说说单例模式!
- On the quantity control mechanism of swoole collaboration creation in production environment
- MySQL version upgrade + data migration
猜你喜欢
![寻找翻转数组的最小值[抽象二分]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
寻找翻转数组的最小值[抽象二分]

line-height小用
![Search rotation array ii[Abstract dichotomy exercise]](/img/db/3ea01cf1ad8446a7007891ef1d8e7f.png)
Search rotation array ii[Abstract dichotomy exercise]

文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测

WINCC与STEP7的仿真连接_过路老熊_新浪博客

文献调研(三):数据驱动的建筑能耗预测模型综述

Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog

数组常用的一些操作方法

Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification

支付宝支付接口沙箱环境测试以及整合到一个ssm电商项目中
随机推荐
数组常用的一些操作方法
Doris 运维中遇到的问题
Recommended system design
Several common rich text editors
Virtual and pure virtual destructors and their implementation in c++
博图软件中多重背景块的建立_过路老熊_新浪博客
Search rotation array ii[Abstract dichotomy exercise]
常用的几款富文本编辑器
Literature research (III): overview of data-driven building energy consumption prediction models
Network protocol: detailed explanation of redis protocol
Unsigned and signed vernacular
对伪类的理解
The role of iomanip header file in actual combat
Stream in PHP socket communication_ Understanding of select method
ASA如何配置端口映射及PAT
php中使用google protobuf协议环境配置
Two ways to center block level elements
MySQL version upgrade + data migration
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
6. common instructions (upper) v-cloak, v-once, v-pre