• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access modifiers for method inner classes

 
rakhee gupta
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the answer should be None
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mamta Sharma
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic