The types of variables supported by the Java language are:
- Class variable: A variable that is independent of the method and is decorated with static.
- Instance variable: A variable that is independent of the method, but has no static adornment.
- Local variable: A variable in a method of a class.
Public classCount {Private Static intstaticnum=0;//Static Variables Private intlvars=0;//instance Variable Public voidmethod () {inti = 0;//Local Variables } PublicCount () { for(intI=0;i<20;++i) {//Cycle 20 timesstaticnum++; Lvars++; } System.out.println ("Static:" +staticnum+ "Lvars:" +lvars); } Public Static voidMain (string[] args) {Count test=NewCount (); Count test2=NewCount (); }}
Execution Result:
static: lvars:20static: lvars:20
Note that static variables are saved in the class, no matter how many instance objects are created, only one staticnum is assigned, plus 20 per creation, and the instance variable is reassigned each time the object is created, and Lvars adds 20 on top of the initial value for each object created.
Java Learning Note one: variable types