Home > Java > javaTutorial > A brief introduction to local variables and global variables in Java

A brief introduction to local variables and global variables in Java

黄舟
Release: 2017-09-22 11:33:52
Original
1633 people have browsed it

This article mainly involves local variables and global variables in Java. It introduces their meaning, survival time and creation location. Friends in need can refer to it.

This article introduces local variables and global variables in java as follows:

1. Local variables:

Definition of local variables: The variables defined in the method are all local variables (the main method is also a method, so the variables defined in the main method are also local variables).

Survival time: The survival time of a local variable is consistent with that of the method. When the method is called to declare and initialize the local variable, the local variable is created and memory space is allocated; until the method is called Ending the local variable is the end;

Does it need to be initialized: Local variables must be initialized before use. The system will not initialize data operations on local variables by default. If the local variables are not initialized before use, Initialization will report an error in the compiler; if the local variable is declared but not initialized, but has not been used, the compiler will not report an error; (local variables must be initialized before use)

Creation location: Local variables are created in stack memory;

2. Global variables:

2.1 Non-static global variables :

The definition of non-static global variables: Non-static global variables are set in the class and are member variables or member attributes of the class that are part of the class (or object part);

Lifetime: Non-static global variables are loaded in heap memory, created with declaration initialization, and die with the death of the object;

Whether Requires initialization: Global variables do not need to be forced to be initialized. The system will assign default values ​​according to their data types by default; however, it is recommended to initialize them when declaring them;

Creation location: Created in heap memory, because the member variables of non-static global variables are part of the object;

2.2 Static global variables:

Definition of static global variables: static class member variables;

Life time: Static global variables are loaded and generated as the bytecode file of the class is loaded, and as the bytecode file is loaded disappears and disappears, and the survival time is longer than the object of the class;
Whether to initialize: All global variables do not need to be initialized, and the same goes for static variables. The system will automatically assign a default value according to its data type, but it is recommended Variables are initialized when declared;

Creation location: Static variables exist in the memory, so static global variables also exist in the heap memory.

Summarize

The above is the detailed content of A brief introduction to local variables and global variables in Java. 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