title: Publicly declare a package private type in a method signature
body: <p>This is possible in Java:</p>
  
  <pre><code>package x;
  
  public class X {
  
      // How can this method be public??
      public Y getY() {
          return new Y();
      }
  }
  
  class Y {}
  </code></pre>
  
  <p>So what's a good reason the Java compiler lets me declare the <code>getY()</code> method as <code>public</code>? What's bothering me is: the class <code>Y</code> is package private, but the accessor <code>getY()</code> declares it in its method signature. But outside of the <code>x</code>package, I can only assign the method's results to <code>Object</code>:</p>
  
  <pre><code>// OK
  Object o = new X().getY();
  
  // Not OK:
  Y y = new X().getY();
  </code></pre>
  
  <p>OK. Now I can somehow try to make up an example where this could somehow be explained with <em>method result covariance</em>. But to make things worse, I can also do this:</p>
  
  <pre><code>package x;
  
  public class X {
      public Y getY(Y result) {
          return result;
      }
  }
  
  class Y {}
  </code></pre>
  
  <p>Now I could never call <code>getY(Y result)</code> from outside of the <code>x</code> package. Why can I do that? <strong>Why does the compiler let me declare a method in a way that I cannot call it?</strong></p>