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


  1. Specify value to field at the same time with field declaration
  2. Fields in the field Assign to constructor?

    What is the difference between the two Two ways to start the field in?

    Not too much! The main difference is that if you provide it at the point of declaration, then it will apply to all constructors, which means that you can forget to add any future constructor initially.

    Take a look at the official mark.

    It is worth noting that if you start the fields on the announcement, it happens that before the constructor starts executing.

    As a rule of thumb, when I initially declare "initialize dead dead" field (when there is no doubt what initial value should be) it "clears" example For

      class some class {list & lt; Integer & gt; CurrentNumbers = New Arrestist & lt; Integer & gt; (); Int counter = 0; // ...}   

    While there is a slight chance that I should behave differently in different constructors, I leave it to the creator.

Comments