Header Ads

Header ADS

Variables in java

Variable-
Variable is the name of the memory location where actual value is stored.
There are three types of variables in java,which are given below such as-

1. Local variable.
2. Instance variable.
3. static variable.


1. Local variable-
A variable declared inside the body of  method is called local variable.
Local variable cannot define with the static keyword. 

2. 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.

It is called instance variable because its value is instance specific and not shared among instance.

3. Static variable-
A variable which is declared as static is called static variable.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 when the class is loaded in the memory.


Example-

class A{
int data=50; //instance variable
static int m = 100 //static variable
void method(){
int n=90;//local variable
}

}//end of class



                             Thank you
     

No comments

for more information plz do comment and follow my blog.

Powered by Blogger.