• 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

Q of inner class in different package

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the following codes to test the access modifiers of inner classes:

When compiled, line1 is ok, but line2 causes a compile error: Inner2() has protected access in p1.A.Inner2(both the code--remove package&import lines--are ok if they were put in the same directory). Where am I wrong? Can we use inner class as general class members? Or using inner class like this is meaningless?
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well member inner classes are like other class member variables, methods, thus to access a protected member from a subclass of another package, you have to access it through an instance of the subclass not the super class.
Thus if you change your code to the following, it should work just fine.

HTH
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Alfred.
I modified the code as you said, I still got the same compile error:
I compiled the 2 files in their parent directory with the following commands:
javac p1\A.java
javac p2\A.java
I think it's right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic