• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic