- If the value of the variable is varied from object to object such type of variables are called instance variables
- For every object a separate copy of instance variable will be created.
- Instance variable will be created at the time of object creation and instance variables will destroy at the time of object destruction. Hence the scope of instance variable is exactly same as the scope of the object.
- Instance variables will be stored in the heap area as the part of the Object.
- Instance variables should be declared within the class directly but outside of any method or block or constructor.
- We can't access instance variables directly from static area but we can access by using object reference.
- We can access instance variables directly from instance area
- class Test{
- int i;
- boolean b;
- double d;
- String s;
- public static void main(String args[]) {
- System.out.println(i); // C.E: Non static variable x can't be referenced from a static context
- Test t = new Test();
- System.out.println(t.i); //0
- System.out.println(t.d); //0.0
- System.out.println(t.b); //false
- System.out.println(t.s); //null
- }
- }
0 comments:
Post a Comment