Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward Refrence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward Refrence" Watch "Forward Refrence" New topic
Author

Forward Refrence

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156


Why the output of above program is 0


Sage of The Monstrous Toad of Mount Myoboku
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
because when you call the getI() the i is initialised to its default value...
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
But i thought that wen getI() method return i it would have been initialized to 5, because int i = 5; statement would be executed first.
Am i right??
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
hey when int j is being executed the line int i=5 isnt executed....that is why the jvm only knows about the variable i...so default value
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
ohhhhhh got it
Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
Concept: The initalization of the variable happen in the order in which the variable appear.
In this case when j is initialized the value of i is initialized to 0.
 
 
subject: Forward Refrence
 
Similar Threads
another forward referencing doubt
why is this legal?
initializers-referencing
Forward reference problem
Request for scrunched up code listings