• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Initializing in a constructor versus a declaration

 
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76819
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76819
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic