Opinion requested: Where to instantiate field variable?

  • Thread starter Thread starter Pluto
  • Start date Start date
P

Pluto

I have a private field like:
private XmlDocument xmlDocument;

In the constructor, I have:
xmlDocument = new XmlDocument();

Is it better to instantiate the XmlDocument in the field declaration like:
private XmlDocument xmlDocument = new XmlDocument();

Does it even matter? What is the order of things happening? Does constructor
or field execute first?

Thanks.
 
One issue to think about is what happens if the new XmlDocument fails. Do
you want to catch and exceptionan, do something different and continue or do
you want to throw a meaningfull exception, etc.

Rob Trainer
 
Back
Top