Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing in a constructor versus a declaration  RSS feed

 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a class that will contain another object as a component. For example, my 'Shop' class will have a 'Manager' object.

Is there any advantage to initializing the Manager in the constructor rather than in the declaration?

e.g.

public class Shop {

Manager mgr = new Manager(); // initialized at declaration

// more shop stuff...

}

versus

public class Shop {

Manager mgr;

public Shop() {
mgr = new Manager(); // initialized in a constructor
}

// more shop stuff...

}

I realise that some initializations are complex (like populating an ArrayList), and some initializations can throw exceptions/errors. That is probably good reason to put these initializations in a constructor. However, for simple declaration/initializations like the one shown above, I can't see the advantage of a constructor over the easier declaration approach.

Thanks in advance to those who answer.

Tom
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually do all my initializations in the constructor. I don't really have a good reason for this, other than the fact that I personally think it makes my code look more clean.

You already mentioned more complex types of initializations. I guess I add simple ones to the constructor as well, simply for the sake of consistency. The only time I combine the declaration and the initialization is when I have multiple constructors, and the members are initialized the same way, regardless of which constructor is called.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a good reason for using the constructor. You can pass arguments which are used as the data in the associated objects. If you initialise immediately on declaration, the state of the Manager object will always be the same. You can give your Manager a name, for example. If you have a Manager(String name) constructor, you should use that. This sort of class is poor design, and error-prone:If the no-args constructor is called, the name field will point to null until a setName() method is called. Then using the equals() method will cause a NullPointerException. It is inconceivable that you would have a Manager whose name you don't know. So the way to prevent that problem is to delete the no-args constructor.

For more protection, change the constructor to something like this:
 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Stephan and Campbell. I was mainly checking that if I initialized at declaration that nothing bad would happen or that I would be violating an OO principle or something like that. Now I know that it is not particularly wrong, however there are good reasons to initialize in the constructor as you have shown. Once again, thanks for taking the time to reply. Tom
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Mato wrote: . . . it is not particularly wrong, however there are good reasons to initialize in the constructor . . .
That looks a good conclusion to have drawn.

And "you're welcome"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!