๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #720

Re: A question about inner classes

#720
From: Carlos Zin
Date: Fri, 03 Dec 2004 22:12
86 lines
3603 bytes
thanks, very helpful your explanation
cz
"dar7yl" <no@reply.org> escribi๏ฟฝ en el mensaje 
news:41b0cd56@newsgroups.borland.com...
> "Carlos Zin" <nomailplease@hotmail.com> wrote in message 
> news:41afa7d8$1@newsgroups.borland.com...
>> Please suppose I have the following case where Tool is a class defined 
>> somewhere else:
>>
>> public class ToolFactory {
>>    private class A extends Tool {}
>>    private class B extends Tool {}
>>    public static Tool makeTool(String toolName) {
>>        if (toolName == "A")
>>            return new A();
>>        else if (toolName == "B")
>>            return new B();
>>        else
>>            return null;
>>    }
>> }
>>
>> I am trying to build a static factory method named "makeTool()" which 
>> produces instances of classes A and B but the compiler complain telling 
>> that non-static variable this cannot be referenced from a static context.
>>
>> Making the method non-static eliminate the error but then I need to make 
>> an instance of Toolfactory in order to access method makeTool(s) which is 
>> not what I want. I need to call "ToolFactory.makeTool(s)", i.e. a static 
>> method with no object instance.
>> How can I solve this problem?
>
> You can make A and B static classes:
>
> <code>
>  private static class A extends Tool {}
>  private static class B extends Tool {}
> </code>
>
> Otherwise, you must have a singleton instance of your ToolFactory class 
> which provides the environment (ie class variables) which can be accessed 
> by your subclasses, if desired.
> viz:
> <code>
> public class ToolFactory
> {
>    private class A extends Tool {}
>    private class B extends Tool {}
>
>    // place class variables here, if desired,
>    // and objects of A and B can access them.
>
>    // static singleton instance
>    private static ToolFactory factoryInstance = null;
>
>    // private constructor enforces Singleton pattern
>    private ToolFactory ()
>    {
>        // Initialize class variables here
>    }
>
>    public static Tool makeTool(String toolName)
>   {
>        // singleton auto-constructor
>        if (factoryInstance ==  null)
>            factoryInstance = new ToolFactory ();
>
>        if (toolName == "A")
>            return new factoryInstance.A();
>        else if (toolName == "B")
>            return new factoryInstance.B();
>        else
>            return null;
>    }  // makeTool
> } // ToolFactory
> </code>
>
>
> regards,
>    Dar7yl
>
>
>
>
>
> 
Technical Details
Message-ID: <41b0e46d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!newsfeed2.dallas1.level3.net!newsreader1.level3.com!newsfeed1.dallas1.level3.net!news.level3.com!wn13feed!worldnet.att.net!216.196.98.144!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <41afa7d8$1@newsgroups.borland.com> <41b0cd56@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,8b4c0601b22a435f
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII
From: "Carlos Zin" <nomailplease@hotmail.com>
Newsgroups: borland.public.jbuilder.java.language
References: <41afa7d8$1@newsgroups.borland.com> <41b0cd56@newsgroups.borland.com>
Subject: Re: A question about inner classes
Date: Fri, 3 Dec 2004 18:12:06 -0400
Lines: 88
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 200.109.212.207
Message-ID: <41b0e46d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102111853 200.109.212.207 (3 Dec 2004 14:10:53 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!newsfeed2.dallas1.level3.net!newsreader1.level3.com!newsfeed1.dallas1.level3.net!news.level3.com!wn13feed!worldnet.att.net!216.196.98.144!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail