Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class does not compile when private - why not?  RSS feed

 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an inner class which compiles when default access inside a method, when private access within a class, but not when private and inside a method. Why not?
Given an interface:

This compiles OK:

and so does this:

but not this (this is IntA with the private keyword added), flagging it :

Compile reports:

Thanks!
Steve
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're defining a local class. Local classes can't have any access specifier, because it doesn't make sense. The class only exists within the method, so nothing outside of the method would be able to access it anyway.
 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Nice. Thanks!
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't write private, public or static inside a method. There are probably other keywords you can't use inside methods, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!