title: Difference member initialisation between C++ and Java
body: <p>Consider the following code snippets:</p>
  <pre><code>class A {
  public:
      A() {cout &lt;&lt; &quot;First creation&quot;;}
      A(int x) {cout &lt;&lt; &quot;Second creation&quot;;}
  };
  
  class B {
      A first;
  public:
      B() {first = A(2);}
  };
  </code></pre>
  <p>When creating a new B object, First and second creation get printed (to avoid this one uses member initialisation lists). However, I can't reproduce this behaviour in java (It only prints &quot;Second creation&quot;):</p>
  <pre><code>class A {
      public A() {System.out.println(&quot;First creation&quot;);}
      public A(int x) {System.out.println(&quot;Second creation&quot;);}
  }
  
  class B {
      A first;
      public B() {first = new A(3);}
  }
  </code></pre>
  <p>This indicates that there is some difference how Java and C++ treat those objects, and since I'm a C++ beginner, it would be interesting to understand why this is the case.</p>