Rules for using variables in java

First, we need to know the syntax for defining variables: data type variable name = value;

classABCStringinfo=null;voiddoWork(Stringname)System.out.println(x);intage;

Variables are divided into two main categories according to where they are defined in the class:

Member variables: global variables/fields (Field), don't call them properties, defined directly in the class, outside the method.

Class member variables: fields modified with static; instance member variables: fields without static modification.

Local variables: Variables are local variables except member variables.

The variables inside the method; the formal parameters of the method; the variables in the code block, that is, the variables in a pair of {}.

The initial value of the variable: the initialization of the variable will open up space in the memory.

Member variable: the default is to have an initial value, see the figure below;

Local variables: have no initial value, so they must be initialized before they can be used.

Open Baidu App to see more beautiful pictures

the initial value of the variable

Scope of variables: Variables are defined differently according to their location, and also determine their respective scopes. It mainly depends on the pair of {} where the variable is located.

Member variables: are valid in the entire class; local variables: start at the beginning of the definition, to the parenthesis immediately following the end.

Member variables can be used first and then defined, and local variables must be defined before they can be used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325381966&siteId=291194637