Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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




 
Saloon Keeper
Posts: 15656
367
  • 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 public, default, protected and private are valid access modifiers for regular inner classes.

Thanks,
Kushan



 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic