[thelist] Java Instantiation

klute soundres9 at yahoo.com
Sun Jun 29 09:42:00 CDT 2003


Keith,

Class.forName("XYZ") is used to dynamically load a
class without hardcoding the class name. Prior to
calling this method, you have to have "XYZ" defined in
your environment (there are multiple ways of doing
this both dynamically and using config files). 

In your case, "org.xml.sax.DocumentHandler" is an
interface that needs to be implemented by a
standard-compliant SAX parser. So, any parser
implementation that implements that interface, can be
"plugged-in" using dynamic class loading features
without having to change the code that contains the
line
Class.forName("org.xml.sax.DocumentHandler").newInstance();

As you can see, there is a great value in doing this
as this guarantees that you are not tying yourself to
any specific parser implementation. 

Last but not least, the DocumentHandler interface
 is a part of SAX1 implementation which was deprecated
in SAX2 and replaced with ContentHandler interface. So
if you are writing new code, I would definitely
recommend using SAX2.

Hope this helps,
James 

--- Keith Dahlby <dahlbyk at softhome.net> wrote:
> Just a quick question about Java object
> instantiation...
> 
> Why would one want to use:
>      DocumentHandler h = 
>
Class.forName("org.xml.sax.DocumentHandler").newInstance();
> 
> instead of:
>      DocumentHandler h = new
> org.xml.sax.DocumentHandler();
> 
> to make an instance of this object (or any, for that
> matter)?
> 
> I've looked in my references and haven't found an
> acceptable answer. TIA!
> 
> Cheers ~ K


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com


More information about the thelist mailing list