Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>static</em> modifier has nothing to do with visibility, so nothing will change. You can't access <em>protected</em> member (i.e. field, method) from different package, no matter if it's static or not.</p> <p>But in your case, MyClass2 extends MyClass1. You can access protected members from superclass. Still, <em>static</em> has nothing to do with this.</p> <p>EDIT:</p> <p>Well, this is a very interesting case because there are involved two separate things: 1) Accessing protected members from subclass in different package, 2) Syntax trick allowing you to access static (class) members like instance members.</p> <p>I will extend your example to show what actually are you doing in this code:</p> <pre><code>package p1; public class MyClass1 { protected String protectedString = "example"; protected static String protectedStaticString = "example"; } package p2; import p1.MyClass1; public class MyClass2 extends MyClass1 { public void testProtected() { MyClass1 otherMC1 = new MyClass1(); MyClass2 otherMC2 = new MyClass2(); String testProtected; testProtected = this.protectedString; // OK, protectedString is inherited, so it's instance member of this class testProtected = super.protectedString; // OK, it's also instance member of superclass testProtected = otherMC1.protectedString; // ERROR. You can't access protected members of other instance of superclass testProtected = otherMC2.protectedString; // OK. As it's inherited member of MyClass2, you can access it if it belongs to other instance of your class } public void testProtectedStatic() { MyClass1 otherMC1 = new MyClass1(); MyClass2 otherMC2 = new MyClass2(); String testProtectedStatic; testProtectedStatic = this.protectedStaticString; // OK - syntax trick testProtectedStatic = super.protectedStaticString; // OK - syntax trick testProtectedStatic = otherMC1.protectedStaticString; // OK - syntax trick testProtectedStatic = otherMC2.protectedStaticString; // OK - syntax trick testProtectedStatic = MyClass1.protectedStaticString; // OK - you can access protected static members from superclass testProtectedStatic = MyClass2.protectedStaticString; // OK - this also static member of your class } } </code></pre> <p>Now, what is 'syntax trick'. Compiler allows you accessing static members like they were instance members:</p> <pre><code>MyClass mc = new MyClass(); Object o = mc.STATIC_FIELD; </code></pre> <p>But what will be actually compiled to the bytecode:</p> <pre><code>MyClass mc = new MyClass(); Object o = MyClass.STATIC_FIELD; </code></pre> <p>Accessing static members in this way is considered as bad practice because it's misleading. You should always access static members in a static way to avoid confusion while reading code.</p> <p>This is what's goin on in your case. There were static field accessed in a non-static way, so it looked like instance field. When you removed <em>static</em> modifier, it became an instance member. The way this field is accessed changed, but you didn't notice that because of your <em>object.staticField</em> syntax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload