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?
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.
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.