Variable is a name. Variable name reserved an area which is allocated in memory.
In java each and every variable has some specific type, this specific type defines the size in variable's memory.
One should declare variables before used.
Eg. int a; // Here a is data type.
a=50; // a is use and initialized as 50.
There are three types of variables in java
Local Variable
Instance Variable
Static Variable
1. Local Variable
Local variable are those variable which are declared inside method, constructors, or blocks. These variables are visible only within the declared method, constructor or block.
2. Instance Variable
A variable declared inside the class but outside the method, constructors, or blocks is called instance variable. such variables did not declared as static. These variables have a default values for example for numbers the default value is 0.
3. Static Variable
It is also known as Class Variable. It is declared with static keyword inside the class but outside method, constructor or a block. When program start static variable created and when program stop it destroyed.
It is used to refer the common property of all objects and memory used only once in class area at the time of class loading i.e it saves memory.
Eg.
class Example
{
int a=10; // Instance Variable
static int b=20; // Static Variable
void method()
{
int c=30; // Local Variable
}
}
0 Comment(s)