• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

What is the access modifier on a regular inner class?

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:
Here is the code for question # 12, chapter 8 ("Inner Classes") from the K&B SCJP book:



The book states that for this code to compile cleanly the inner class Sorter must be declared static. OK, so that means that class Sorter as coded is not static, so then what is its access modifier? Is its access modifier default since there was no access modifier provided? Does this mean that all regular inner classes have default access, unless an access modifier is provided?

I think I know the answer to this but I need someone who knows much more than I do to confirm.

Thank you all for your time.
G




 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Sorter has default (package private) access. Keep in mind that this has nothing to do with static, because static is not an access modifier. Declaring Sorter as static still means it has package private access.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gary,

You are talking about two different aspects. In your example why the class is not compiled is not because of insufficient access privileges. But because of "static methods can only access static variables or methods".

Consider regular inner class as merely a member in the outer class. If you think like that we can specify any access modifier for regular inner class. That is coderanch, default, protected and private are valid access modifiers for regular inner classes.

Thanks,
Kushan



 
reply
    Bookmark Topic Watch Topic
  • New Topic