• 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

Is This Incorrect??

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The foll question is from Bill Brogden's Exam Cram chp 5,p.94 Q(4)
class NormalClass{
static class NestedClass{
}
which of the following is correct way to declare and initialize a ref to a Nested class object
(a)
NormalClass.NestedClass mync = new
NormalClass.NestedClass();// ans.
(b)
NestedClass myNC = new NormalClass().new NestedClass()// ok incorrect
(c)
NestedClass myNC = new NormalClass.NestedClass();/* why this is incorrect??? Is this not a correct way to reference a static class ?
for a top level nested(static)class, you dont need a ref to enclosed class, so even this should work
NestedClass nc1 = new NestedClass();
any explanation would be appreciated.*/
}
}

------------------

[This message has been edited by Shiva (edited April 12, 2000).]
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer C is not correct because the top-level inner class is not a member of its enclosing class. It might be easier to think of the enclosing class as an enclosing scope for the nested class in this case.
Answer A is an example of this. NormalClass is the enclosing scope of NestedClass so saying new NormalClass.NestedClass() is calling the constructor of NestedClass with the NormalClass scope tacked on front.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shiva: Your question has typos. Do you want us to correct them too? Not fair
All the best...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John!
Tony i was trying to compile that example , and just inserted my comments into it(not a good coding practice ha!)
i will watch typos,
Thanks

------------------
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic