• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

access modifiers for method inner classes

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the answer should be None
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic