title: Java/C++ Object creation order
body: <p>Please take a look at the following piece of code:</p>
  
  <pre><code>class Parent {
      Parent() {
          printFunction();
      }
  
      public void printFunction() {
          System.out.println("Parent Print");
      }
  
      class ParentInner {
         ParentInner() {
            InnerPrint();
         }
  
         void InnerPrint() {
            System.out.println("Parent Inner print");
         }
      }
  }
  
  class Child extends Parent {
      ChildInner ci;
      Child() {
          super();
          ci = new ChildInner();
      }
  
      @Override
      public void printFunction() {
          System.out.println("Child Print");
      }
  
      class ChildInner extends ParentInner {
         ChildInner() {
            super();
         }
  
         @Override
         void InnerPrint() {
            System.out.println("Child Inner print");
         }
      }
  
      public static void main(String[] args) {
          Child c = new Child();
      }
  }
  </code></pre>
  
  <p>The output of this code from a Java compiler (both eclipse and linux) is:</p>
  
  <pre><code>Child print
  Child Inner print
  </code></pre>
  
  <p>This, in C++ (gcc)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;new&gt;
  using namespace std;
  
  class Parent {
      public:
      Parent() {
          printFunction();
      }
  
      virtual void printFunction() {
          cout &lt;&lt; "Parent print\n";
      }
  
      class ParentInner {
         public:
         ParentInner() {
            InnerPrint();
         }
  
         virtual void InnerPrint() {
            cout &lt;&lt; "Parent Inner print\n";
         }
      };
  };
  
  class Child : public Parent {
      public:
      Child():Parent() {
          ci = new ChildInner();
      }
  
      void printFunction() {
          cout &lt;&lt; "Child print\n";
      }
  
      class ChildInner : public ParentInner {
         public:
         ChildInner():ParentInner() {
         }
  
         void InnerPrint() {
            cout &lt;&lt; "Child Inner print\n";
         }
      };
  
      ChildInner *ci;
  };
  
  int main(int argc, char* argv[]) {
     Child *c = new Child();
     return 0;
  }
  </code></pre>
  
  <p>prints:</p>
  
  <pre><code>Parent print
  Parent Inner print
  </code></pre>
  
  <p>What say? I think the base class gets created first, then the child- gcc is logical. What's up in Java?</p>