"One more question if I may. Why is the mySAXParserFactory instantiated through a static newInstance()
method instead of through its constructor?"
To avoid your code having compile time dependencies on a specific SAXParserFactory
class. If you look at the javadoc for the newInstance()
method, you will see that it allows you to configure different parser factory classes via a system property, a property file or the Services API.
"Is this a Singleton?"
No. The Javadoc says "This static method creates a new factory instance." (my emphasis)
"Ok, I'll ask a more general question. Why are some classes in Java instantiated through a constructor and others - through static methods (like the .newInstance() method mentioned here)?"
Different classes have different use-cases. For example, you would not want or need to implement your application so that someone could choose between using a hash map or a tree map for some data structure. However, you may well want someone to be able to select a different XML parser.
The purpose of the "factory object" and "factory method" patterns is to avoid having a hard dependency on specific object classes at object creation time. Sometimes this is a good thing, but in other situations the factory is just "dead weight". You need to learn ... by experience ... when to use factories, and when not to.