| Author |
access modifiers for method inner classes
|
rakhee gupta
Ranch Hand
Joined: May 01, 2008
Posts: 43
|
|
Hi, In K&B page 646 it is mentioned that "the same rules apply to method-local inner classes as to local variable declarations. You can't, for example,mark a method-local inner class public, private, protected, static, transient,and the like. The only modifiers you can apply to a method-local inner class are abstract and final, but as always, never both at the same time. But when I tried this piece of code with different modifiers it worked: public class MyClass1 { public static void main(String argv[]){ } /*Modifier at XX */ class MyInner {} } What modifiers would be legal at XX in the above code? 1) public 2) private 3) static 4) friend Class MyInner is in main() method so it should only allow final or abstract modifier but it is allowing pub;ic and private modifier also. Can anyone explain this.Is this a error in K&B
|
 |
Ralph Jaus
Ranch Hand
Joined: Apr 27, 2008
Posts: 342
|
|
|
Your class MyInner is not in main method. Otherwise the compiler would complain an illegal start of expression, if you used for example the public modifier.
|
SCJP 5 (98%) - SCBCD 5 (98%)
|
 |
Sandeep Bhandari
Ranch Hand
Joined: Apr 16, 2004
Posts: 201
|
|
|
So the answer should be None
|
SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
|
 |
Ashish Hareet
Ranch Hand
Joined: Jul 14, 2001
Posts: 375
|
|
Rakhee, Take careful note of class MyInner in your code. It is not a local class, it is in fact a member inner class because it doesn't exist in the main method. Try the following instead When writing code place your curly braces on newlines instead of putting them at the end of lines & use tab's to indent your code. It'll help you when debuging such things. HTH Ashish Hareet Edited to include code formatting. [ July 31, 2008: Message edited by: Ashish Hareet ]
|
 |
vaibhav mishra
Ranch Hand
Joined: Jun 18, 2008
Posts: 168
|
|
Originally posted by rakhee gupta: public class MyClass1 { public static void main(String argv[]){ }[qb]main ends here[qb] /*Modifier at XX */ class MyInner {} [qb]//this is not in main[qb] } What modifiers would be legal at XX in the above code? 1) public 2) private 3) static 4) friend Class MyInner is in main() method so it should only allow final or abstract modifier but it is allowing pub;ic and private modifier also. Can anyone explain this.Is this a error in K&B
this might clear some of your confusion [ August 01, 2008: Message edited by: vaibhav mishra ]
|
SCJP
|
 |
Mamta Sharma
Greenhorn
Joined: Jun 03, 2008
Posts: 25
|
|
Hi Rakhee, In your question MyInner class is not in main method rather it is an inner class defined in the boundries of MyClass1 class.So it can be defined with any access specifiers like public,protected,private,strictfp,static,final abstract. A method local inner class can only use final or abstract. I think I helped you out to find your answer. [ August 01, 2008: Message edited by: Mamta Sharma ]
|
 |
 |
|
|
subject: access modifiers for method inner classes
|
|
|