1

Possible Duplicate:
Instance variable initialization in java
When are initializations outside a constructor called?

Is there any difference between A1 and A2?

class A1 {
   B b = new B();
   A1() {
   }
}

//and  

class A2 {
   B b;
   A2() {
      b = new B();
   }
}

I want to know when the class B b does its construction if I instantiate the A1 and A2 classes.

Community
  • 1
  • 1
CuGBabyBeaR
  • 303
  • 4
  • 12

3 Answers3

1

There is no difference between them. The compiler automatically puts the instance variable initialization inside every constructor you declare, within an initializer block.

So, your first way, after compilation becomes:

class A1 {
   B b;
   A1() {
      {
         b = new B();
      }
   }
}

But, the 2nd way is more readable.

From the book Java in a nutshell - Chapter#3:

Field declarations, however, are not part of any method, so they cannot be executed as statements are. Instead, the Java compiler generates instance-field initialization code automatically and puts it in the constructor or constructors for the class. The initialization code is inserted into a constructor in the order it appears in the source code, which means that a field initializer can use the initial values of fields declared before it.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Personally I find the 1st way more readable. Largely because the initialization code is completely obvious, especially if there are multiple constructors and especially when they can't call each other - you then have code duplication. And what would happen if you removed all constructors - you'd have to use the 1st way. Also, the 2nd way is simply more lines of code adding no value = code bloat. If I saw version 2 in my code base I would refactor to version 1 without hesitation, and scold the programmer who wrote it. – Bohemian Oct 20 '12 at 10:38
  • @Bohemian. As far as code duplication is concerned, as I said, compiler move the initialization in each of the constructor.. So that part is not the reason for using the first one. – Rohit Jain Oct 20 '12 at 10:43
  • @Bohemian. But using 1st one or the 2nd one is always a programmer's own choice. That will not make much of a difference. – Rohit Jain Oct 20 '12 at 10:45
1

In both the class B b does its construction when you invoke the constructor of A1 and A2, as the constructor is used to initialize non static data member and B b is reference type of data member. If you define

class A1{
  static B b=new B();
  A1(){
  }
}

then it would load at class loading time. The compiler will load class B and make an instance of B at class loading time else there is no difference between two.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Arun
  • 1,038
  • 2
  • 12
  • 25
  • Your answer is rather poorly worded, using properly spelling and grammar is encouraged here. Perhaps edit it for a clean up? – Michael Berry Oct 20 '12 at 10:22
0

Not in the example you've given - there's no difference. They'll both initiate a reference to B on object creation.

However, in terms of style I would personally lean towards A2.

If we unpack class A1 a bit more, it's equivalent to this:

class A1 {
   B b;

   {
       b = new B();
   }

   A1() {
   }
}

The unnamed block of code in the above is the initialisation block, a block of code which is run each and every time an object is created (irrespective of the called constructor, and it runs before the constructor.) It's semantically equivalent to putting all the statements in it at the start of each constructor, which brings us back to A2. Therefore, they're identical.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216