When does Java create a symbol table of the class variables being used? The variable occupancyPerRoom is not declared until (3), then how does the method initMaxGuests() know that is even exists?
posted 12 years ago
Hi Cathy I have summarize the list of things that happen when an object of a class is created. It has been derived from the same book that you have referred. I would request others to correct me if I'm wrong as I'm no expert in Java. Here's my list (note that the order of steps is important): 1. Static variable initializer expressions and static initializer blocks are executed in the order they are specified. 2. All the instance variables are initialized to their default values (eg. integers are initialized to 0, strings to null, etc etc) 3. The appropriate class constructor is invoked. This can lead to the local chaining of constructors. The constructor at the end of the chain invokes the super class constructor (implicitly or explicitly). (Constructor chaining ensures that the inherited state of the object is constructed first). 4. Initialization of the instance member variables by executing their instance initializer expressions and any instance initializer blocks in the order they are specified. 5. Execution of constructor body. Therefore, if we apply the above list of actions to your example: - At step 2, occupancyPerRoom is initialized to 0. - At step 4, the expression maxNoOfGuests = initMaxGuests () is executed, which prints the value of occupancyPerRoom as 0. I'm not sure if I answered your question correctly but I tried Thanks Harwinder [ October 19, 2003: Message edited by: Harwinder Bhatia ]