Variables
Variable is name of reserved area allocated in memory.
Example:
Example:
int var=20; //Here var is a variable
There are three types of variables in java:
There are three types of variables in java:
- local variable
- instance variable
- static variable
A variable that is declared inside the method is called local variable.
Instance Variable
- A variable that is declared inside the class but outside the method is called instance variable.
- It is not declared as static.
A variable that is declared as static is called static variable. It cannot be local.
Example to understand the types of variables
class A
{
//instance variable
int data=50;
//static variable
static int m=100;
void method()
{
//local variable
int n=90;
}
//end of class
}
class A
{
//instance variable
int data=50;
//static variable
static int m=100;
void method()
{
//local variable
int n=90;
}
//end of class
}