• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

about forward reference of variables (mock test question)

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {

int i = getX ( ) ;
int j = 10 ;

int getX ( ) {
return j ; //1
}

public static void main ( String args [ ] ) {
System . out . println ( new Test ( ) . i ) ;
}
};

Options :

a . Compiler error - can't call getX ( ) before the constructor completes initialization
b . Compiler error - variable j may not have been initialized
c . Compiles fine & prints 10
d . Compiles fine & prints 0


output d . Compiles fine & prints 0

I think there is a forward reference at //1 as j. But apprently i am worng ... so please someone tell me the real program flow !

Thanks
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this link solve your doubt?

Forward Referencing


Thanks,
[ July 04, 2007: Message edited by: Chandra Bhatt ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also doubt about this question.
Can someone tell the program flow.
Thank you!
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article may be helpful too: Forward Referencing
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic