File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

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: 19896
    
  20

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Order of Variable declaration