title: Why do I get the compiler error "No enclosing instance of type ex4 is accessible"
body: <p>I'm having trouble in my <code>main</code> method. On the line <code>MyInt r1 = new MyInt(2);</code> I get a compiler error with the message: "No enclosing instance of type ex4 is accessible". </p>
  
  <pre><code>public class ex4 {
      public class MyInt implements Comparable&lt;MyInt&gt; { 
          private int value;
  
          MyInt(int x){ this.value= x;}
  
          public String toString() {
              return ("Result: " + intValue());
          }
  
          public int intValue() { return value; }
  
          public int compareTo(MyInt rhs){
              if ( value &gt;rhs.value ){
                  return 1;}
              if (value &lt; rhs.value){
                  return -1;}
              else return  0;
          }
      }
  
      public static void main(String[] args) {
          MyInt r1 = new MyInt(2); //Error here ("No enclosing instance of type ex4 is accessible. Must qualify the allocation with an enclosing instance of type ex4 (e.g. x.new A() where x is an instance of ex4)."
          MyInt r2 = new MyInt(7); // same here
          System.out.println(r1.compareTo(r2));
      }
  }
  </code></pre>
  
  <p>I've seen other responses to similar questions but I'm unable to solve my own issue, could anyone help? I'm trying to compare one value to another.
  Thanks.</p>