2

This is probably a 'duplicate', but I'm not sure how to search for this question...

I am initializing a non-static member variable at the declaration line:

public class A
{
    private B b = new B();
    ...
}

I am doing it instead of initializing this variable inside the default constructor:

public class A
{
    private B b;
    public A()
    {
        b = new B();
    }
    ...
}

Is there any difference between the two, except (perhaps) the former not being "ANSI Java" or something like that?

I am getting two different byte-codes (i.e., two different 'class' files) for the two implementations above, which leads me to believe that there might be run-time differences.

So I would like to know if I have any reason to expect anything different during run-time.

Thanks

barak manos
  • 29,648
  • 10
  • 62
  • 114

5 Answers5

3

The first one is a declaration and initialization at the same time .

In the second example instead you have the declaration of the b variable not initialized and then in the constructor you initialize the variable ...

The functional difference can come at the moment that you add another constructor, in that case the b variable should be initialized even in that constructor or if not, there will be a huge difference .

In the first case you have the variable initialized no matter how many constructor you implement ...

Honestly I don't understand how can you pretend to have the same bytecode, writing two different things as for this case .

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • But this is a NON-STATIC member variable. The moment I create an instance of `A`, this variable will be initialized inside or outside the constructor. Perhaps you mean that the difference is in the order of the operations? First variable `b` is initialized and then method `A()` is called, or vice-versa? – barak manos Jan 12 '14 at 17:21
1

There is one small difference between this approaches!

When variable is declared inside constructor, there is chance that after some time second constructor will be created and this variable will be uninitialized. For fighting this declare this variable as final - if this is possible of course ;)

Other differences not exists :)

public class A
{
    private B b;
    public A() {
        b = new B();
    }
    public A(int value) { // second constructor
    }
    ...
}

After using A a = new A(5); field b is null.

MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

No different in general (mean that declare outside and inside a constructor will behave something different). Just remind that initialize outside will run first, before come into some specific constructor. For example:

class A {
  int a = 3;
  public A() {
     a = 4;  // now a = 4. not 3
  }
}

But, I often use them with different purpose :

  1. Initialize variables inside constructor make it clearer and help you initialize something more complex. for example, put some logic code, put a loop to add items, ... that you cannot do when initialize outside of constructor scope.

  2. When you have many overloading constructors, and some variables always declare same. Simple "state variable" such as isExist isEmpty ... I often initialize outside of constructor scope. So, all other constructors don't do the same thing.

hqt
  • 29,632
  • 51
  • 171
  • 250
1

There are no difference, both codes work well. I personally prefer the second way for a big class, meanwhile the first is preferd for small classes.

Federico Ponzi
  • 2,682
  • 4
  • 34
  • 60
0

The main difference is the order of function calls:

  • In the 1st case, method B() is called before method A().
  • In the 2nd case, method A() is called before method B().

An additional difference is what has been suggested in all other answers...

When a non-default constructor which does not initialize variable b exists:

  • In the 1st case, variable b will be initialized even when that constructor is used.
  • In the 2nd case, variable b will not be initialized whenever that constructor is used.
barak manos
  • 29,648
  • 10
  • 62
  • 114