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

InnerClass issue

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
public class NormClass {
long startTime ;

public class NestedClass {
// methods and variables of NestedClass
}
// other methods and variables of NormClass
}

Which of the following can be used by a method inside NestedClass to refer to the startTime variable in the enclosing instance of NormClass?

A

this.startTime

B

NormClass.this.startTime

C

this.NormClass.startTime

D

startTime

[code]

I have come across with this sort of question several times but answers are inconsistent.
The actual answer what I think are B and D. But in case the inner class has already a member named startTime, then simply writing startTime wont get you outer class startTime. In this regard option "B" is the only correct answer.

What should I suppose before answering this kind of questions? Should I look for inner class, where it has a member named startTime or NOT?

Please guide...


Source


Thanks,

[EDITED] Post edited one Keith's reminder regarding answer 'C'

[ May 01, 2007: Message edited by: Chandra Bhatt ]
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be B and D always. The inner class can always access variables of the outer class

You might want to call NestedClass "InnerClass"

Also the link to the Source doesn't work.
[ May 01, 2007: Message edited by: swarna dasa ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure C is correct?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would not suppose that the InnerClass re-defines

long startTime ;

unless the inner class code shows that or question explicitly tells me that ....

So in this scenario B & D would be correct.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic