Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward Refrence

 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why the output of above program is 0
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because when you call the getI() the i is initialised to its default value...
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhhhh got it
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic