File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid pg 333-334 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid pg 333-334" Watch "Khalid pg 333-334" New topic

Khalid pg 333-334

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

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?
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
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
[ October 19, 2003: Message edited by: Harwinder Bhatia ]
I agree. Here's the link:
subject: Khalid pg 333-334
It's not a secret anymore!