aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class & Anonymous class : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class & Anonymous class :" Watch "Inner class & Anonymous class :" New topic
Author

Inner class & Anonymous class :

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
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

Joined: Jul 13, 2001
Posts: 94
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

Joined: May 10, 2001
Posts: 3244
Geetha
Chack out the JLS section 8.1.2 for details on inner classes.
Vaibhav Shridish
Greenhorn

Joined: Jun 06, 2002
Posts: 28
Well i really dont understand wht u r speaking ..
Plz refer section 7.3 Mughal and Ras..
Nonstatic inner class cannot have static members ...


Vaibhav Shridish
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner class & Anonymous class :