Re: Creating funny objects
From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 12/06/04
- Next message: Michael Borgwardt: "Re: Zip/UnZip char []"
- Previous message: Michael Borgwardt: "Re: Java speed vs. C++."
- In reply to: hopkins: "Creating funny objects"
- Next in thread: greg_at_alcorgrp.com: "Re: Creating funny objects"
- Reply: greg_at_alcorgrp.com: "Re: Creating funny objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 06 Dec 2004 11:51:35 +0100
hopkins wrote:
> In particular, why are objects created like this
>
> SAXParserFactory factory = SAXParserFactory.newInstance();
> ...and
> SAXParser saxParser = factory.newSAXParser();
>
> I have always been taught to creat object in the form
>
> Person newPerson = new Person();
>
> This way where you seem to call a different method other than the
> constructor when the object is created confuses me and I cant really
> understand why you'd want to do this.
This is known as a factory method. Paul gave an example why one might
want to do this.
Basically, it's more flexible than a constructor, because a constructor
can only "return" an instance of the concrete class, while a factory can
declare an abstract class or an interface as its return type and return
an arbitrary subclass or intrface implementation. Note that in your
code, SAXParser is an abstract class, so it's not possible to instantiate
it with "new".
> Secondly as the two lines in
> question dont seem to have the "new" keyword does this mean they are
> not created on the heap and are stack based like in C++?
No. The factory method will use "new" in some way, directly or indirectly.
- Next message: Michael Borgwardt: "Re: Zip/UnZip char []"
- Previous message: Michael Borgwardt: "Re: Java speed vs. C++."
- In reply to: hopkins: "Creating funny objects"
- Next in thread: greg_at_alcorgrp.com: "Re: Creating funny objects"
- Reply: greg_at_alcorgrp.com: "Re: Creating funny objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|