title: Getting hold of the outer class object from the inner class object
body: <p>I have the following code. I want to get hold of the outer class object using which I created the inner class object <code>inner</code>. How can I do it?</p>
  
  <pre><code>public class OuterClass {
  
      public class InnerClass {
          private String name = "Peakit";
      }
  
      public static void main(String[] args) {
          OuterClass outer = new OuterClass();
          InnerClass inner = outer.new InnerClass();
         // How to get the same outer object which created the inner object back?
          OuterClass anotherOuter = ?? ;
  
          if(anotherOuter == outer) {
               System.out.println("Was able to reach out to the outer object via inner !!");
          } else {
               System.out.println("No luck :-( ");
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Well, some of you guys suggested of modifying the inner class by adding a method:</p>
  
  <pre><code>public OuterClass outer() {
     return OuterClass.this;
  }
  </code></pre>
  
  <p>But what if I don't have control to modify the inner class, then (just to confirm) do we have some other way of getting the corresponding outer class object from the inner class object?</p>