I have the following code in Java:
package a; public classs ClassInOtherPackage{ protected void protectedMethod(){} protected class protectedInnerClass {} } package b; import a.*; public class MyClass extends ClassInOtherPackage{ public MyClass(){ MyClass x = new MyClass(); x.protectedMethod(); //<-- This one ok //UPDATED: Declaration is ok MyClass.protectedInnerClass y; //<-- This one ok //UPDATED: Only when instantiated is not allowed, why? y = x.new protectedInnerClass(); //<-- Not allow when instantiated. } } Refer to the Java documentation:
"The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package."
Why can't I instantiate the inner protected class as shown above?