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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top