Following steps should be executed when the program runs:
1. Static variables get initialized when the class is first loaded. Hence, si being the static variable gets the value 0 & is stored on the stack. A new object 'so' is created in the heap & its reference is stored on the stack.
2. When control is transfered to main, again a new object 'o' is created in the heap & its reference is stored on the stack.
3. Variables ii and io remain uninitialized as no instance of class 'A' is created yet.
4. Also, variable j is uninitialized.
In pts 3,4, we have 3 uninitialized variables. Not sure if they are allocated any space anywhere. Somebody plz clarify.
Originally posted by Sharn Arora: Hence, si being the static variable gets the value 0 & is stored on the stack.
This is incorrect. Static fields are not on the stack, but I don't think they are on the heap either. Static fields a.k.a. class fields are special in that they are part of the class not the objects that belongs to that class. If 'si' would have been an instance field it would have been stored on the heap together with the object it is part of. However, if I remember correctly, they are on the 'method area' together with the rest of the class definition such as the class's methods..