Why are protected instance members not visible inside a subclass within a different package, but protected class members are?

мајдејсаремех :
package one;

public class A {
    protected int first;
    protected static int second;
}

package two;

import one.A;

public class B extends A {
    public void someMethod() {
        this.first = 5; //works as expected
        B.second = 6; //works
        A a = new A();
        // a.first = 7; does not compile

        //works just fine, but why?
        a.second = 8; 
        A.second = 9;
    }
}

Why aren't the same restrictions applied to static fields, what's the idea behind it?

Vince Emigh :

From JLS 6.6.2:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

From 6.6.2.1:

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

this.first = 5; works because B is an implementor of A.

A.second works because this restriction is only defined for members of an object. Same goes for B.second.

As for why it's specified this way, you'd have to ask the people defining the specification - we could only make assumpsions. 6.6.2.1 even has an example that expresses an issue similar to this:

Consider this example, where the points package declares:

package points;
public class Point {
    protected int x, y;
    void warp(threePoint.Point3d a) {
        if (a.z > 0)  // compile-time error: cannot access a.z
            a.delta(this);
    }
}

and the threePoint package declares:

package threePoint;
import points.Point;
public class Point3d extends Point {
    protected int z;
    public void delta(Point p) {
        p.x += this.x;  // compile-time error: cannot access p.x
        p.y += this.y;  // compile-time error: cannot access p.y
    }
    public void delta3d(Point3d q) {
        q.x += this.x;
        q.y += this.y;
        q.z += this.z;
    }
}

A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). The method delta3d can access the protected members of its parameter q, because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d.


I recommend checking out Why we should not use protected static in Java.

The semantics of protected were aimed at instance members - protected static is contradictive to the purpose of protected, which could be why it wasn't restricted in the same manner.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=468264&siteId=1