MY mENU


Thursday 9 February 2012

Instance Variable in java

An Instance Variable is a variable whose separate copy is available every object.
- So if the Instance Variable value is modified in one object, it will not effect the value of variable in other objects.
Instance Variables are created in Heap Area of JVM.
- We can call the Instance Variables using Object Reference only

Example : Class A {
int p;
}
Class B {
A a = new A();
a.p = 10;
}




A Static Variable is a variable whose single copy in memory is shared by all the Objects.
- So if any modification to Static Variable will effect the values of variables in all the objects.
- Static Variables are created in Method Area of JVM.
- we can call the Static Variables using Class name itself and Object Refernce also.


Example : Class A { 
static int p; 
}
Class B { 
A.p = 10; 
}

No comments:

Post a Comment