[thelist] Java Instantiation

klute soundres9 at yahoo.com
Sun Jun 29 16:48:04 CDT 2003


Hey Keith,

--- Keith Dahlby <dahlbyk at softhome.net> wrote:
> On a side note, I'm trying 
> to learn SAX2...do you have any recommended
> resources?
>
Well, you can check out: http://xml.apache.org/ to
read about Xerces 2 and Xalan 2 for Java. Both support
SAX2 and DOM2 standards and even v3 working drafts of
the spec. 

An excellent book I have and recommend is Java and XML
by Brett McLaughlin (O'Reilly).
http://www.amazon.com/exec/obidos/tg/detail/-/0596001975/qid=1056922109/sr=8-1/ref=sr_8_1/002-2051692-0255269?v=glance&s=books&n=507846

>  
>
> The instantiation question arises from the code I'm
> trying to optimize. The 
> relevant code is:
> DocumentHandler h =
>      (DocumentHandler) Class
>          .forName("xacute.util.ToStringHandler")
>          .newInstance();
> String nodeFactory =
> "xacute.impl.xdom.NodeFactoryXerces";
> NodeFactory nf =
>      (NodeFactory)
> Class.forName(nodeFactory).newInstance();
> 
> As the interface must be closely coupled with
> Kweelt's libraries 
> (xacute.*), I should be safe to use this instead:
> DocumentHandler h = new
> xacute.util.ToStringHandler();
> NodeFactory nf = new
> xacute.impl.xdom.NodeFactoryXerces();
> 
> The code I'm trying to optimize was written by the
> professor of a Database 
> Systems class I'm taking...I'm just trying to make
> sure that I'm not 
> missing something. Thanks again for your help.
>
I don't thinks that Class.forName().newInstance()
presents noticeable performance problems. However, if
you look at the source of java.lang.Class (in
particular, getInstance() and getInstance0() methods),
you'll see that there is quite a bit happenning there
(mostly security checks). So, performance will
probably be affected a little (milliseconds, I think).
Try outputing System.currentTimeMillis() before and
after the instantiating using two ways to see the
difference. 

In the code you posted, there is, obviously, very
little sense in using dynamic class loader since the
class names are hardcoded in that same Java file.

James
  
> At 09:42 AM 6/29/2003, you wrote:
> >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
> 

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


More information about the thelist mailing list