protected references in Java
This question already has an answer here:
 A protected member can be accessed in a subclass outside the package only through inheritance.  Try this instead :  
public class C extends A {
    void foo() {
       int b = i;  
    }
}
There are no needs to make a reference every time. I think you didn't understand Inheritence..
public class B extends A {
    void foo() {
       // A a = new A(); No need to instantiate it here as B extends A
        int b = i;  //No nedd to refer it through a.a.i  
    }
}
package another.pac;
public class C extends A {
    void foo() {
        C c=new C();
        int d=c.i//this will work fine
       //   A a = new A(); same as above explanation
        int b = i;  //same as above and even a.i will not compile
    }
}
Now Your protected variable will be accessible here..
Class A is apart of package pac;
and Class C is apart of package another.pac therefore it will be unable to access its member. If C is apart of package pac then it would be able to access the member
See the following post: In Java, difference between default, public, protected, and private
链接地址: http://www.djcxy.com/p/24076.html上一篇: 在java类中声明一个变量(private,static,final)
下一篇: Java中受保护的引用
