开发者

what's the difference between those two ways to initialize fields in Java?

  1. Assign value to field at the same time with field decla开发者_Python百科ration
  2. Assign value to field in the constructor?


What's the difference between those two ways to initialize fields in Java?

Not much! The main difference is that if you assign it at the point of declaration, it will apply to all constructors. That is, there's no way you can forget to add the initialization to any future constructors.

Have a look at the official Initializing Fields trail.

May be worth noting that if you initialize the fields at the declaration, it happens before the constructor starts executing.

As a rule of thumb, I usually initialize the "dead simple" fields at the declaration (when there is no doubt what the initial value should be). This "cleans up" For instance

class SomeClass {

    List<Integer> currentNumbers = new ArrayList<Integer>();
    int counter = 0;

    // ...
}

while as if there is the slightest chance that I may want different behaviors in different constructors, I leave it to the constructor.


In both cases, the value will only be assigned after the superclass constructor has executed.

For the first option, you have to be able to determine the value without reference to any constructor parameters.

For the second option, you'll need to assign the value in every constructor overload.

Basically I tend to favour the second option when the value depends on constructor parameters (usually that's all I would want it to depend on) and the first option when the value will be the same for any newly initialized instance. I tend not to mix and match for a single field, using an initializer with the declaration and also assigning it in some constructor overloads, but even that can be useful occasionally. (Imagine a collection which has a size of 0 for most constructors, but has one constructor which takes an initial set of values.)

While you can refer to other instance members in variable declarations, I prefer not to - calling instance methods when your object is only partially initialized is brittle, and referring to other variables relies on the variable ordering, which feels ugly to me.


(1) is a syntax sugar for (2) (with the exception of static fields)


It does exactly the same, just at different times in the object instantiation life-cycle. Have a look here for more information.


In the first case the field will be created when the class is initialised and assigned directly the value you hace declared for it, while in the second case the field will be created and assigned a default value (null if it's an object, 0 if it's an int, etc.) and then assigned the correct value when the constructor is executed. Of course practically there's usually not much difference for you since the result in both cases is the same: when you create an instance of this class after the constructor returns the field is initialised correctly. I guess the first approach would be more usefull if you have more than one constructors and a field which should have the same value regardless of which constructor has been called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜