Variables in Java
- A variable is a container which holds the value.
- Variable is a name of memory location.
- It is a combination of "vary + able" that means its value can be changed.
Types of Variables
- Local variable
- Instance variable
- Static variable
Local Variable
A variable declared inside the body of the method.
It cannot be defined with "static" keyword.
Instance Variable
A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.
Static Variable
A variable which is declared as static.
It cannot be local.
You can create a single copy of static variable and share among all the instances of the class.
Memory allocation for static variable happens only once.
Example :
class A
{
int data=50; //instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
}//end of class
0 Comments