• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Inner Classes and Visibility

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Those who are studying for SCJP should analyze this piece of code carefully and try to predict the output.

Now for the question...
How does one print the value of i at 1 in the statement marked 2 ?
Regards,
Jayesh
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm.. interesting one!
Anyways the output is straightforward but ur second questtion is tricky!
2 1 & anonymous 5 1 5 2
I guess what happens here is that once you enturn into return
it hides the value if i even though it is final. Though you cannot chnage its value before that. I do no t belive ypu can print it's value unless you declare a diff varibale i1 and store it in that. Anyways would love to hear from other guys out there!


 
jayesh bindra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone else ???
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, the output is 5142, not 5152.
I can't see any way for the variable i at //1 to be visible at //2,
The Final modifier lets the inner class access i, it being a local variable inside the method defining the inner class. But as soon as the inner class defines its version of i, the local i at //1 is totally inaccesable (unless you cheat and change it from i to j).
I'd love someone to prove me wrong.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't int i=5; in the display() method throwing error ?
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,
I'm getting an compilation error at
int i = 5 ;
error : Variable i is already defined in this method
if i comment this line evertthing is fine & the o/p is
2 1
anonymous0102
Aruna
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aruna,
I had the same problem when I used jdk1.2.1
jdk1.3 compiles fine
Vidya
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Guys,
The output which i got is
2 1
anonymous5142
can any one tell me why others got 'anonymous5152' i think '4' is correct because inside anonymous class 'i' variable is having value '4'.
if i am wrong please explain to me....
regards
Satish
 
Would you like to try a free sample? Today we are featuring tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic