Private Member Access Java

后端 未结 7 2261
盖世英雄少女心
盖世英雄少女心 2020-12-05 16:29

Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile

    class Pr         


        
7条回答
  •  独厮守ぢ
    2020-12-05 17:10

    As DevSolar has said, it's at the (top level) class level.

    From section 6.6 of the Java Language Specification:

    Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

    Note that there's no indication that it's restricted to members for a particular object.

    As of Java 7, the compiler no longer allows access to private members of type variables. So if the method had a signature like public void messWithI(T t) then it would be a compiler error to access t.i. That wouldn't change your particular scenario, however.

提交回复
热议问题