• 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

Inner class & Anonymous class :

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of these statements r true?
1.Non static inner classes can't have static members.
2.Objects of top level nested class can be created without creating an outer instance.
3.Member variables in any nested class can't be declared final.
4.Anonymous class can't have constructors.
5.Anonymous class can't be static.
I thought the answers were 1,2,4.
But the answer is given as 2,4,5.
Anonymous class can be static,right:in the sense the context in which it's declared determines that.Though we don't explicitly use static to declare it so.
Pl.clarify,thanks in advance.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Non static inner classes can't have static members.
False. They can have static memebers but those members must be final.
2.Objects of top level nested class can be created without creating an outer instance.
True. A top level nested class is like any other top level class - there is no enclosing instance.
3.Member variables in any nested class can't be declared final.
False. Sure that can, and in the case of static variables they must be final.
4.Anonymous class can't have constructors.
True. They have no name so they can't have a constructor.
5.Anonymous class can't be static.
True. There is nowhere in the class declaration to put the static keyword. However, it can be declared in a static context (in a static method) this might be a semantic issue
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Geetha!
The first statement
1. Non static inner classes can't have static members.
is wrong because you can have
public static final in non static inner classes, i.e. you can declare static final constants.
Brian
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave and Brain for the clarification.
Got it now .
By the way,where can i look up for such details reg.inner classes and anonymous class?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geetha
Chack out the JLS section 8.1.2 for details on inner classes.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i really dont understand wht u r speaking ..
Plz refer section 7.3 Mughal and Ras..
Nonstatic inner class cannot have static members ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic