Variable
Variables in Java programs can be broadly divided into member variables and local variables .
Local variables
formal parameter : a local variable defined in a method signature, which is the responsibility of the method caller to assign a value to it, which dies with the end of the method.
local variables within a method: Local variables defined within a method must be explicitly initialized within a method. This type of local variable takes effect from the completion of initialization and dies with the end of the method.
Local variables within code blocks : Local variables defined within a block of code that must be explicitly initialized within a block of code. This type of local variable takes effect from the completion of initialization and dies with the end of the block of code.
Local variables are short-lived, and they are stored in the stack memory of the method.
Member variables
A variable defined in a class is called a member variable (English is field).
If the member variable is defined without a static decoration, the member variable is also known as a non-static variable or an instance variable , and if a static decoration is used, the member variable can also be called a static variable or a class variable. (The initialization time of a class variable is always before the initialization of the instance variable)
static can only decorate member parts that are defined in a class, including member variables, methods, inner classes, initialization blocks, and internal enumeration classes.
Public classVariable {//member Variable age intAge ; //class variable name StaticString name; //str Parameters Public voidShow (String str) {//local variables in the method intMethod = 0; Static inttest=123;//here, error. Static local variableSystem.out.println (str+method); } {//code blockString Codeblock= "code block"; } Public Static voidMain (string[] args) {Static inttest=123;//here, error. Static local variable }}
Variables in Java