Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize class fields in constructor or at declaration?

Tags:

java

I've been programming in C# and Java recently and I am curious where the best place is to initialize my class fields.

Should I do it at declaration?:

public class Dice {     private int topFace = 1;     private Random myRand = new Random();      public void Roll()     {        // ......     } } 

or in a constructor?:

public class Dice {     private int topFace;     private Random myRand;      public Dice()     {         topFace = 1;         myRand = new Random();     }      public void Roll()     {         // .....     } } 

I'm really curious what some of you veterans think is the best practice. I want to be consistent and stick to one approach.

like image 546
mmcdole Avatar asked Aug 23 '08 19:08

mmcdole


People also ask

Should I initialize variable within constructor or outside constructor?

If you know what value a final variable will have at declaration, it makes sense to initialize it outside the constructors. However, if you want the users of your class to initialize the final variable through a constructor, delay the initialization until the constructor.

Where should you initialize variables in a class?

Discussion. You should always initialize native variables, especially if they are class member variables. Class variables, on the other hand, should have a constructor defined that will initialize its state properly, so you do not always have to initialize them.

Can you initialize variables in class declaration?

To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.

Do fields have to be initialized when they are declared?

It is only necessary that they be declared and initialized before they are used.


2 Answers

My rules:

  1. Don't initialize with the default values in declaration (null, false, 0, 0.0…).
  2. Prefer initialization in declaration if you don't have a constructor parameter that changes the value of the field.
  3. If the value of the field changes because of a constructor parameter put the initialization in the constructors.
  4. Be consistent in your practice (the most important rule).
like image 91
kokos Avatar answered Sep 28 '22 10:09

kokos


In C# it doesn't matter. The two code samples you give are utterly equivalent. In the first example the C# compiler (or is it the CLR?) will construct an empty constructor and initialise the variables as if they were in the constructor (there's a slight nuance to this that Jon Skeet explains in the comments below). If there is already a constructor then any initialisation "above" will be moved into the top of it.

In terms of best practice the former is less error prone than the latter as someone could easily add another constructor and forget to chain it.

like image 21
Quibblesome Avatar answered Sep 28 '22 09:09

Quibblesome