Home > Java > javaTutorial > body text

What is the concept of local variables in java

WBOY
Release: 2023-06-03 18:57:34
forward
1941 people have browsed it

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);
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template