wood burning stoves*
The moose likes Beginning Java and the fly likes Order of Variable declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Order of Variable declaration" Watch "Order of Variable declaration" New topic
Author

Order of Variable declaration

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Hi

i have question about the following program.

Why the output is During Object followed by static instead of other way?

Since static variable is initialized first it should be displayed as static first followed by during Object right?



please explain


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

When the class is loaded, b is initialized. For that, it first creates an instance of the class, and that instance initializes its member "a". So the following is the exact order:
- creation of new Test instance
- initialization of its member "a" - first call to disp()
- that instance is used to call disp() again
- the result of that method is assigned to b


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks I got it.
 
jQuery in Action, 2nd edition
 
subject: Order of Variable declaration