Should I instantiate instance variables on declaration or in the constructor?

Is there any advantage for either approach?

Example 1:

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

Example 2:

class A {
    B b;

    A() {
         b = new B();
    }
}

15 Answers
15

Leave a Comment