Home >Java >javaTutorial >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!