This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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!

 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic