Home  >  Article  >  Java  >  What is the concept of local variables in java

What is the concept of local variables in java

WBOY
WBOYforward
2023-06-03 18:57:341891browse

Explanation

1. Local variables are declared in methods, structural methods or statement blocks;

2. Local variables are declared in execution methods, structural methods or sentences Block is created, and the variable will be destroyed after execution is completed;

Access modifiers cannot be used for local variables;

Local variables can only be seen in the method, structure method or sentence block where they are declared. To;

Local variables are allocated on the stack;

Because local variables do not have default values, local variables must be initialized after they are declared.

Example

public class Demo{
    //main 方法
    public static void main(String[] args){
        //声明一个局部变量i
        int i;
        //局部变量不能直接使用,需要初始化值以后才可以使用,否则会报错
        System.out.println(i);
        
        //声明并初始化值
        int j = 10;
        System.out.println(j);
    }
}

The above is the detailed content of What is the concept of local variables in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete