🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: A question about inner classes

#714
From: Lori M Olson [TeamB]
Date: Thu, 02 Dec 2004 23:49
59 lines
2828 bytes

Carlos Zin wrote:
> 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?
> 
> 
> 

I just ran into this.  It's maddening and embarrassing all at the same 
time.  You need to use:

return new ToolFactory.A();
return new ToolFactory.B();

-- 

Regards,

Lori Olson [TeamB]

------------

Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.

Google Advanced Newsgroup Search
     http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
     http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
     http://www.visi.com/~gyles19/fom-serve/cache/1.html
Technical Details
Message-ID: <41afa9f3@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!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
Date: Thu, 02 Dec 2004 16:49:08 -0700
From: "Lori M Olson [TeamB]" <javadragon@techie.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax; CDonDemand-Dom)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: A question about inner classes
References: <41afa7d8$1@newsgroups.borland.com>
In-Reply-To: <41afa7d8$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 142.179.181.200
Message-ID: <41afa9f3@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102031347 142.179.181.200 (2 Dec 2004 15:49:07 -0700)
Lines: 60
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail