1. Variable is a name of memory location where data is stored.
2. The value of variable can vary, means value can be changed.
There are three types of variables :
1. Local Variables :
1.1 DECLARED : inside methods, constructors or blocks.
1.2 SCOPE : within the methods, constructors or blocks but not outside of them.
1.3 MEMORY ALLOCATION : when method, constructor or block is invoked variable is allocated; when exits, variable gets destroyed.
1.4 STORED MEMORY : stack memory
1.5 DEFAULT VALUES : doesnt have any default value, so it should be assigned before use.
1.6 ACCESS MODIFIERS : public, private or protected access specifier cannot be used.
2. Instance Variable :
2.1 DECLARED : in a class but outside methods, constructors or blocks.
2.2 SCOPE : inside the class, within all methods, constructors or blocks.
2.3 MEMORY ALLOCATION : when object is created, variable allocated memory; when object destroyed, memory releases.
2.4 STORED MEMORY : heap memory
2.5 DEFAULT VALUES : have default values eg. int has 0 value, boolean false, String null etc.
2.6 ACCESS MODIFIERS : any access modifiers can be used.
2.7 HOW TO ACCESS : a) can be accessed directly by calling the variable name inside the class.
b) in static methods they should be called using the fully qualified name, ie objectRefName.variable_name.
3. static variable :
3.1 DECLARED : with static keyword in a class but outside methods, constructors or blocks.
3.2 SCOPE : similar to instance variable ie inside the class, within all methods, constructors or blocks including static part
3.3 MEMORY ALLOCATION : when we run program and .class file is loaded, variable allocated; when class file unload, variable gets destroyed.
3.4 STORED MEMORY : non-heap memory or static memory
3.5 DEFAULT VALUES : have default values eg. int has 0 value, boolean false, String null etc.
3.6 ACCESS MODIFIERS : any access modifiers can be used.
3.7 HOW TO ACCESS : a) directly
b) by using class name
b) by using object reference name