Access to protected . What differencies which link use(parent or child)?

Please help me with this issue:

consider this code

import java.util.Observable;
import java.util.Observer;

public class MyObservable extends Observable {
    int k;
    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
        setChanged();   //valid
    }

    public static  void   main(String [] args){
        Observable observable = new MyObservable();

        Observer myObserver1 = new MyObserver();
        Observer myObserver2 = new MyObserver();

        observable.addObserver(myObserver1);
        observable.addObserver(myObserver2);

        observable.setChanged(); //compilation error here
        observable.notifyObservers();
    }
}

I invoke setChanged() from public method(it is valid) and from public static method(it is not valid(I see message that it is protected method and it is not vald)). I thought that static/non static has not affect to access.

I cannot understand why.

I'm waiting for your right explanation.

I want to know why java founders Do made it so? What are conceptually wrong in my variant?

Jon Skeet
people
quotationmark

It's not really a static/non-static difference - it's a difference in the compile-time type of the reference you're trying to call the method on.

In the instance method, you're implicitly calling it on this, which has a compile-time type of MyObservable. In main, you're calling it on a reference of type Observable, and you can't do that because of how protected access works.

From the JLS, section 6.6.2 (emphasis near the end is mine):

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.

6.6.2.1. Access to a protected Member

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.

In addition, if Id denotes an instance field or instance method, then:

  • If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

  • If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

So in this case, if you change the declared type of observable to MyObservable, you'll find the error goes away.

people

See more on this question at Stackoverflow