🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: A question about inner classes

#719
From: dar7yl
Date: Fri, 03 Dec 2004 20:32
76 lines
3195 bytes
"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: <41b0cd56@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news-in.ntli.net!newsrout1-win.ntli.net!ntli.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41afa7d8$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,8b4c0601b22a435f
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "dar7yl" <no@reply.org>
Newsgroups: borland.public.jbuilder.java.language
References: <41afa7d8$1@newsgroups.borland.com>
Subject: Re: A question about inner classes
Date: Fri, 3 Dec 2004 12:32:27 -0800
Lines: 82
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.6.189.203
Message-ID: <41b0cd56@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102105942 207.6.189.203 (3 Dec 2004 12:32:22 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news-in.ntli.net!newsrout1-win.ntli.net!ntli.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail