Home > Java > javaTutorial > The difference between java global variables and local variables

The difference between java global variables and local variables

(*-*)浩
Release: 2019-12-26 14:14:57
Original
7600 people have browsed it

The difference between java global variables and local variables

Local variables (Recommended learning: java course )

Local variables are also called internal variables. Local variables are defined within the function. Its scope is limited to the function, and it is illegal to use such variables after leaving the function.

Global variables

Global variables are also called external variables, which are variables defined outside the function. It does not belong to a function, it belongs to a source program file. Its scope is the entire source program.

When using global variables in a function, the global variable description should generally be used. Only global variables declared within a function can be used. The specifier for global variables is extern. However, global variables defined before a function can be used within the function without further explanation.

The difference between global variables and local variables:

1. Different scopes

Global variables = available in the entire class

Local variables = available within this method

2. Different initial values ​​

Global variables = have initial values ​​

Local variables =No initial value

3. Different declarations

Global variables =It is not allowed to declare variables with the same name in a class

Local variables =It is not allowed to declare variables with the same name in a method

4. Different priorities

When two types of variables have the same name, the priority level of local variables

The above is the detailed content of The difference between java global variables and local variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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